简体   繁体   English

jQuery .load:刷新页面时加载同一页面的任何方法

[英]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: 因此,我有一个网站,可将页面加载到容器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). 我的问题是,每刷新页面一次,加载的内容就会重置为默认页面(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... 不仅是localStore,还需要更改url的哈希,然后执行一个函数来捕获哈希代码并在您执行“ goto”函数时执行...

"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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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