2012-03-29 44 views
0

我有一個對我的網站很重要的代碼,它適用於所有瀏覽器,但似乎在Chrome中觸發兩次。當我刷新頁面時會觸發兩次,但當我使用ajax/jquery將內容加載到頁面上的主div時不會觸發。但是,它在每個其他瀏覽器中都可以很好地工作,並且不會兩次觸發這裏是我的代碼:Chrome中的jquery函數加載/觸發

<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> 

如果有人知道如何解決這個問題,我將非常感謝幫助。謝謝!

+0

代碼的哪個部分正在觸發兩次?整個腳本或特定的功能? – mellamokb 2012-03-29 21:27:52

+0

這是初始頁面加載,但它現在已經修復,感謝您的幫助。 – Eggo 2012-03-29 21:32:00

回答

1

你應該總是將jQuery代碼放在jQuery內置的「ready」塊中。

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

這會自動處理相關的「就緒」事件。