2011-02-13 113 views
3

我有與click事件的鏈接在我的JQuery,類似下面:調用頁面後,jQuery函數加載

$('#TopLoginLink').click(function() 
{ 
//Here I want the current page should be reloaded with "https" and after the page is loaded then it will automatically call my function MyDialogPopup(). Something like below: 
var myCurrentPage = window.location.href.replace('#',''); 
       var reloadURL; 

       if (https!=' ') 
       { 
        reloadURL = myCurrentPage.replace('#','').replace("http",https); 
        window.location.href = reloadURL; 
        MyDialogPopup(); 

       } 

}); 

上面的代碼重新加載我的網頁完美,但我的問題是,我的函數也被稱爲immediatley,但我希望我的函數只有在我的頁面完全加載了「https」時纔會被調用。

請建議!我怎麼能做到這一點

+0

定義 「完全」 - 這是否包括圖像和樣式表?如果是,請使用`load()`;如果沒有,使用`.ready()`:http://api.jquery.com/ready/ – 2011-02-13 09:38:54

+0

什麼,當我的登錄鏈接點擊頂部,它重新加載相同的頁面與「https」 – 2011-02-13 09:44:25

回答

2

你應該定義一些標誌。例如把東西放在cookie中或追加到url。然後,在你的JavaScript中定義ready處理那麼當加載頁面時將被稱作:

$(document).ready(function() { 
    if (checkFlag()){ 
     MyDialogPopup(); 
     clearFlag(); 
    } 
}); 

你的處理器將尋找這樣的:

if (https!=' ') 
{ 
    reloadURL = myCurrentPage.replace('#','').replace("http",https); 
    setFlag(); 
    window.location.href = reloadURL; 
} 

如果您堅持使用cookies,您可以使用jquery.cookie插件。這裏是代碼setFlagcheckFlagclearFlag

function setFlag(){ 
    $.cookie('show_dlg', 'true'); 
} 

function clearFlag(){ 
    $.cookie('show_dlg', null); 
} 
function checkFlag(){ 
    return $.cookie('show_dlg') == 'true'; 
} 
0

你可以修改後放在$(document).readyMyDialogPopup方法調用,而不是window.location.href

$(function() { 
    if (do some checks on the url if we need to call the method) { 
     MyDialogPopup(); 
    } 
}); 
+0

我已經得到我在$(document).ready – 2011-02-13 09:42:49

0

我相信這個代碼執行一次加載頁面:

$(document).ready(function() {} 
1
$(document).ready(function() { 
    // all your code here 
}); 
相關問題