简体   繁体   English

带有jQuery的历史记录API-地址栏更改但不刷新div

[英]History API with jQuery - address bar changes but doesn't refresh divs

I've been able to easily use the history.pushState to change the content of my div and have the address bar reflect a fake url. 我已经能够轻松地使用history.pushState更改div的内容,并使地址栏反映一个伪造的url。 However, whenever I push the back/forward buttons the url changes back but the content remains the same. 但是,每当我按下“后退/前进”按钮时,URL都会变回原先的位置,但内容保持不变。 Typing in the url comes up with an error. 在URL中输入错误。 It seems I have the first part of the history API down, but need help doing state changes. 看来我的历史记录API的第一部分已关闭,但需要帮助进行状态更改。

I'm a fairly new programmer and trying to build my website in jQuery and keep the code as concise as possible. 我是一个相当新的程序员,并且尝试在jQuery中构建我的网站并尽可能简化代码。

HTML code: HTML代码:

<ul>
<li><button class="navButton" id="signUp" href="./content/registration.php" name="registration" title="Registration">Sign Up</button></li>
<li><button class="navButton" id="settings" href="./content/account_settings.php" name="settings" title="settings">Settings</button></li>
<li><button class="navButton" id="about" href="./content/about.php" name="about" title="About">About</button></li>
</ul>

<div id="mainContent"></div>

Javascript code: JavaScript代码:

$(document).ready(function() {
    // INITIAL CONTENT ON FIRST LOAD
$("#mainContent").load('./content/start.php');

// CODE FROM HISTORY.JS 
(function(window, undefined) {
var History = window.History; 
if (!History.enabled) {
    return false;
}

History.Adapter.bind(window, 'statechange', function() { 
    var State = History.getState(); 
    History.log(State.data, State.title, State.url);
    });

    // EVENT LISTENER FOR NAVBUTTON CLASS TO REPLACE CONTENT IN MAINCONTENT DIV
$('.navButton').click(function(e) {
    e.preventDefault();
    pageurl = $(this).attr('name');
    $("#mainContent").fadeOut().load($(this).attr("href")).fadeIn();
    if (pageurl != window.location) {
        history.pushState('', $(this).attr('title'), $(this).attr('name'));
        }
    });

})(window);
});

I have installed history.js but if I don't need to use it, that would be preferred. 我已经安装了history.js,但是如果我不需要使用它,那将是首选。 I would love to have this code corrected so the back button refreshes and this works! 我希望纠正此代码,以便后退按钮刷新,并且可以正常工作!

So I solved this by adding an if statement under the popstate to compare the url and determine the content loaded in the mainContent div. 因此,我通过在popstate下添加一个if语句来比较此URL,并确定在mainContent div中加载的内容来解决此问题。 Works!.. but discovered my original pushstate is pushing several popstates the more you click the navButtons. 工作! That question is posted Cannot find where I have a popstate loop creating multiple history entries 该问题已发布找不到我在哪里有创建多个历史记录条目的popstate循环

window.addEventListener('popstate', function() {
    //WHEN BACK/FORWARD CLICKED CHECKS URL PATHNAME TO DETERMINE WHICH CONTENT TO PLACE IN DIV
    if (location.pathname == "/index.php") {
        $("#mainContent").load("./content/start.php");
    } else {
        $("#mainContent").load("./content" + location.pathname + ".php");
    }
});

Advice: use the built html5 history state, to pass in objects or params or so on... 建议:使用内置的html5历史记录状态,以传递对象或参数等。

Action when you are pushing a state into browser: 将状态推入浏览器时的操作:

history.pushState('{url:location}', $(this).attr('title'), $(this).attr('name'));

Action when your clicked back or front using the browser: 使用浏览器向后或向前单击时的操作:

$(window).on('popstate', function(e){
        if(e.originalEvent.state != null){
              console.log("url to render"+e.originalEvent.state.url)
            }else{
              console.log("nothing in the state, do nothing")
            }

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

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