Calling a Jquery function after the pages is loaded
I have got a link with the click event in my JQuery, something like below:
$('#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();
}
});
The above code is reloading my page perfectly, however my problem is that my function is also cal开发者_如何学Cled immediatley, but I want my function should be called only when my page is loaded with "https" completely.
Please suggest! how can I achieve this
You should define some flag. For example put something in cookies or append to url. Then, in your javascript define ready
handler then will be called when page is loaded:
$(document).ready(function() {
if (checkFlag()){
MyDialogPopup();
clearFlag();
}
});
Your handler will be looking like this:
if (https!=' ')
{
reloadURL = myCurrentPage.replace('#','').replace("http",https);
setFlag();
window.location.href = reloadURL;
}
If your stick with cookies, you can use jquery.cookie plugin. Here is code for setFlag
, checkFlag
, clearFlag
:
function setFlag(){
$.cookie('show_dlg', 'true');
}
function clearFlag(){
$.cookie('show_dlg', null);
}
function checkFlag(){
return $.cookie('show_dlg') == 'true';
}
$(document).ready(function() {
// all your code here
});
You could put the MyDialogPopup
method call in $(document).ready
and not after modifying the window.location.href
:
$(function() {
if (do some checks on the url if we need to call the method) {
MyDialogPopup();
}
});
I believe this code executes once the page is loaded:
$(document).ready(function() {}
精彩评论