jQuery - run a function from another page

后端 未结 3 1824
死守一世寂寞
死守一世寂寞 2021-01-07 12:50

I have a account page with links that load the pages with the .load. I am wondering how I can get to the load account info page from another page (not from the account page

3条回答
  •  梦毁少年i
    2021-01-07 13:48

    If you want to run specific scripts on one page from the link on another, you could use hash tags. Append something like #load-account-info to the end of the URL:

    Account Information
    

    Then check for it on account.html:

    $(function() {
    
        // show account info if #showAccountInfo is present
        showAccountInfo();
    
        // detect url hash changes and fire off showAccountInfo
        $(window).bind("hashchange", showAccountInfo());
    
    });
    
    function showAccountInfo() {
        if (window.location.hash == "load-account-info") 
            loadAccountInformation();
    }
    

    Note: not all browsers support the "hashchange" event. As such, there is a jQuery plugin that adds support.

    Edit: added support for detecting clicks on the same page.

    Sources: Working with single page websites and maintaining state using a URL hash and jQuery, On - window.location.hash - Change?

提交回复
热议问题