简体   繁体   English

历史API问题

[英]History API issues

Im make some ajax calls in my website and im trying to implement the history API to make it so I can navigate it with the browser history. 我在网站上进行了一些Ajax调用,并尝试实现history API使其实现,以便我可以使用浏览器历史记录对其进行导航。 This is the code for the "back button": 这是“后退按钮”的代码:


$(document).ready(function(){
  window.addEventListener("popstate", function(e) {

    //Flag I use to not fire it on the first page load
    if (!ajaxhistory)
      return;

    do_my_ajaxs_things();

  }, false);
});

But I have 2 problems 但是我有两个问题

1) When I type the URL in the address bar to load the page for the first time. 1)当我在地址栏中键入URL来首次加载页面时。 This also fires the ajax call, which is undesired obviously. 这也会触发ajax调用,这显然是不希望的。 Theres no need to ajax, since I have to load the entire page. 无需ajax,因为我必须加载整个页面。 This one, I have managed to solve it with a flag, but I wonder if theres a more elegant way to do it 我设法用一个标志解决了这个问题,但我想知道是否有更优雅的方法

2) Lets say I was in "google.com" and the I type my URL "www.mysite.com", then I make an ajax call and I go to "www.mysite.com/contacts". 2)假设我在“ google.com”中,然后输入URL“ www.mysite.com”,然后进行ajax调用,然后转到“ www.mysite.com/contacts”。 If I press BACK button once, i will go to "www.mysite.com" allright, but if I press BACK again, I will still be in "www.mysite.com", and I find myself I cant go back to google. 如果我按一下BACK按钮一次,我会转到“ www.mysite.com”,但是如果我再按一次BACK,我仍然会在“ www.mysite.com”,我发现自己无法回到Google 。 How could I solve this issue? 我该如何解决这个问题?

All help appreciated. 所有帮助表示赞赏。 Thanks. 谢谢。

I think that your approach is wrong. 我认为您的方法是错误的。 You shouldn't need to do AJAX requests each time the user goes back - that's what the state is for, you should have all the relevant data already there. 您不需要每次用户返回时都进行AJAX请求-这就是状态的含义,您应该已经拥有所有相关数据。 IMHO the logic should be the following: 恕我直言,逻辑应如下:

  • If the window loads and window.history.state is already set - just apply this state. 如果窗口加载并且window.history.state已经设置-请应用此状态。
  • Otherwise trigger an AJAX request to retrieve the default state and replace the current state with it. 否则,触发AJAX请求以检索默认状态并将其替换为当前状态。
  • Whenever the user navigates to a new "page", trigger an AJAX request to retrieve the new state and push it. 每当用户导航到新的“页面”时,触发AJAX请求以检索新状态并将其推送。
  • In your popstate handler you should simply apply the state you got in event.state , without doing any new AJAX requests. popstate处理程序中,您应该仅应用在event.state获得的状态,而不执行任何新的AJAX请求。

Here is some example code with a fake loadPage function, normally you would put your AJAX call there. 这是带有伪造的loadPage函数的一些示例代码,通常您会在此处放置AJAX调用。 The applyState function and state data are also absolutely minimal. applyState函数和状态数据也绝对最小。

<script type="text/javascript">
window.onload = function()
{
  if (window.history.state)
  {
    applyState(window.history.state);
  }
  else
  {
    loadPage(0, function(state, title, loc)
    {
      window.history.replaceState(state, title, loc);
    });
  }

  window.addEventListener("popstate", function(event)
  {
    applyState(event.state);
  });
}

function goToPage(pageId)
{
  loadPage(pageId, function(state, title, loc)
  {
    window.history.pushState(state, title, loc);
  });
}

function loadPage(pageId, callback)
{
  window.setTimeout(function()
  {
    var state = {text: "text" + pageId};
    applyState(state);
    callback(state, "page " + pageId, "#" + pageId);
  }, 100);
}

function applyState(state)
{
  document.getElementById("content").textContent = state.text;
}
</script>
<div id="content">
  ???
</div>
<button onclick="goToPage(1);">1</button>
<button onclick="goToPage(2);">2</button>
<button onclick="goToPage(3);">3</button>

For your first problem, this is really a browser issue. 对于您的第一个问题,这实际上是浏览器问题。 A browser should (in my opinion) never fire the popstate event when the page is loaded initially. 在我看来,浏览器应该永远不要在初始加载页面时触发popstate事件。 I think the best thing you can do is only register the event after the page has been loaded. 我认为您能做的最好的事情就是仅在页面加载后注册事件。

History.js is a good library which smoothes out the history API quite a bit. History.js是一个很好的库,可以使history API更加平滑。 Even if you don't use it they have good documentation about the API here: https://github.com/browserstate/history.js/wiki/The-State-of-the-HTML5-History-API 即使您不使用它,也可以在此处获得有关API的良好文档: https : //github.com/browserstate/history.js/wiki/The-State-of-the-HTML5-History-API

About your second issue, the browser wil just go to google instead of firing the popstate event for your website. 关于第二个问题,浏览器只会转到google而不是为您的网站触发popstate事件。 You don't have to worry about that, it's the browsers responsibility 您不必担心,这是浏览器的责任

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

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