Call jQuery function after loading pages

I have a link with a 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 reloads my page completely, however my problem is that my function is also called immediate, but I want my function to be called only when my page is fully loaded with "https".

Please suggest! how can i achieve this

+3
source share
4 answers

You must define some flag. For example, put something in cookies or add a URL. Then in your javascript define a handler ready, after which it is called when the page loads:

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

Your handler will look like this:

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

, jquery.cookie. setFlag, checkFlag, clearFlag:

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

function clearFlag(){
    $.cookie('show_dlg', null);
}
function checkFlag(){
    return $.cookie('show_dlg') == 'true';
}
+2
$(document).ready(function() {
    // all your code here
});
+1

You can put a method call MyDialogPopupin $(document).ready, and not after the change window.location.href:

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

I believe this code is executed after the page loads:

$(document).ready(function() {}
0
source

Source: https://habr.com/ru/post/1791813/


All Articles