2011-08-30 125 views
2

我regestering一个jQuery的fancybox像这样:检查jQuery“Fancybox”是否已经注册?

 $(document).ready(function() { 

     $("#help").fancybox({ 
      'width': '90%', 
      'height': '90%', 
      'autoScale': true, 
      'transitionIn': 'elastic', 
      'transitionOut': 'none', 
      'titleShow': false, 
      'type': 'iframe' 
     }); 
     }); 

然而,在页面传送/回发是越来越多次注册,减慢下来,以突破点。有没有办法检查事件是否已经注册,如果没有,注册?

伪代码:

//check if fancybox has not been registered 
if($("help").fancybox == null)) 
{ 
    //register fancy box 
} 

回答

3

当运行的fancybox,它增加了一个fancybox进入jQuery的data对象。

可以测试它的存在:

if(! $("#help").data().fancybox) 
{ 
    //register fancy box 
} 
+1

Fancybox 2似乎并没有这样做。至少不是以我的经验。 –

1

此功能查找您的#help元件并加载只有当它的存在fancybox.js。成功加载Fancybox.js后,它会调用成功函数并初始化你的fancybox。

这只会影响具有元素#help的页面。所以你保存了HTTP请求和带宽。

请参见:http://api.jquery.com/jQuery.ajax/

你也应该寻找error function它允许你来处理错误。

$(document).ready(function() { 

    // Look for #help 
    if ($('#help').html() !== null) { 

     // Load, init and config the fancybox 
     $.ajax({ 
      url: '/js/path/to/fancybox.js', 
      type: 'GET', 
      dataType: 'script', 
      success: function() { 

       // Init Fancybox 
       $("#help").fancybox({ 
        'width': '90%', 
        'height': '90%', 
        'autoScale': true, 
        'transitionIn': 'elastic', 
        'transitionOut': 'none', 
        'titleShow': false, 
        'type': 'iframe' 
       }); 
      } 
     }); 

    } 
}); 
2

Fancybox使用$(element).data()来存储其元素相关配置。你只需要检查一个fancybox是否存在。

$("#fancy").data("fancybox"); 

查看此fiddle了解更多fnu。

0

有点晚,但对于2的fancybox我用:

parent.jQuery.fancybox.isOpen 

我认为你也可以使用:

parent.jQuery.fancybox.isOpened

两个语句都将返回如果属实fancybox在网站上打开。