【问题标题】:Jquery function loading/firing twice in ChromeJquery函数在Chrome中加载/触发两次
【发布时间】:2012-03-29 21:26:32
【问题描述】:

我有一个对我的网站至关重要的代码,它适用于所有浏览器,但似乎在 Chrome 中触发了两次。当我刷新页面时它会触发两次,但当我使用 ajax/jquery 将内容加载到页面上的主 div 时不会触发。但是,它在所有其他浏览器中都能完美运行,并且不会触发两次。这是我的代码:

<script type='text/javascript'>
$("a").live('click', function(){
history.pushState({}, '', this.href);
popstate(this.href);
return false;
});
$(window).load(function(){
window.onpopstate = function(event){
popstate(window.location.href);
event.preventDefault();
}
popstate = function(url){
url = '/ajaxlinks/ajaxlink'+window.location.pathname.substr(1);
if (url == '/ajaxlinks/ajaxlink'){url = '/ajaxlinks/ajaxlinkindex.php'}
if (url.indexOf(".php") == -1){url = '/ajaxlinks/ajaxlinkprofile.php'}
if (url == '/ajaxlinks/ajaxlinkaccountrecovery.php'){window.location =   'http://www.pearlsquirrel.com/accountrecovery.php';}
if (url == '/ajaxlinks/ajaxlinklogin.php'){window.location =   'http://www.pearlsquirrel.com/login.php';}
if (url == '/ajaxlinks/ajaxlinklogout.php'){window.location =    'http://www.pearlsquirrel.com/logout.php';}
$('#ajaxloadcontent').load(url);
$('html, body').animate({scrollTop:0}, 'fast');
}
});
</script>

如果有人知道如何解决此问题,我将不胜感激。谢谢!

【问题讨论】:

  • 代码的哪一部分准确地触发了两次?整个脚本,还是一个特定的函数?
  • 这是初始页面加载,但现在已修复,感谢您的帮助。

标签: jquery ajax


【解决方案1】:

您应该始终将 jQuery 代码包含在 jQuery 的内置“就绪”块中。

$(function(){
    //all code here
});

这会自动处理相关的“就绪”事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多