简体   繁体   中英

jQuery .load: any way to load the same page when page is refreshed

So I have a website that loads pages to a container div:

function goto(addr) {
    $("#content").load(addr);
}

and a link that executes it

<a href="#" id="aboutus" onclick="goto('page/aboutus.php');">About us</a>

My problem is that whenever the page is refreshed, the loaded content resets to the default page (page/home.php). How could I do so that it loads the previous displayed page?

Use local storage for example or sessions. Local storage example:

$(document).ready(function() {

    var lastPage = localStorage['lastPage'];
    if (!lastPage) { // If user was on any url before we will exectue goto function
        goto(lastPage)
    }

    function goto(addr) {
         localStorage['lastPage'] = addr; // Set url to local storage before load page
         $("#content").load(addr);
    }

});

not only localStore but you need change the hash of url, and after do one function to catch hash code and execute at you "goto" function...

"something like that"

function hashnav(){
   var hashfull = document.location.hash
   var hash = hashfull.replace('#', '');
   var $page = '';
   goto(hash);
}
function changeHash($hash) {
   window.location.hash = $hash;
}

function goto(addr) {
   changeHash(addr);
}

$(window).bind( 'hashchange', function() {
   hashnav();
   return false;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM