我可以阻止history.popstate在初始页面加载时触发吗?

发布于 2024-10-20 09:53:43 字数 482 浏览 8 评论 0原文

我正在开发一个通过 AJAX 提供内容的网站。

如果您单击菜单中的某个项目,内容 div 会使用 $.get 响应进行更新,没什么花哨的。

我正在实现 history.pushState 以允许使用浏览器的后退/前进按钮进行导航。

我有以下内容来加载历史导航上的内容:

$(function() {
    $(window).bind("popstate", function() {
        $.getScript(location.href); 
    });
});

问题是,当第一次加载页面时,此函数执行 $.getScript ,因此页面会立即再次加载。第一次加载页面时,它会呈现初始 HTML 视图,然后在第二次加载时,它会呈现 JS 视图,因为它是 JS 请求。

如何防止此事件在包含 HTML 请求的页面上触发?

I'm working on a site that serves content via AJAX.

If you click an item in the menu, a content div gets updated with $.get response, nothing fancy.

I'm implementing history.pushState to allow navigation with the browser's back/forward button.

I have the following to load content on history navigation:

$(function() {
    $(window).bind("popstate", function() {
        $.getScript(location.href); 
    });
});

The problem is, when a page is loaded for the first time, this function does $.getScript so the page is loaded immediately again. The first time the page is loaded it renders the initial HTML view, then on the second load it renders the JS view, since its a JS request.

How could I prevent this event from firing on pages with HTML requests?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧夏天 2024-10-27 09:53:43

使用本机 HTML5 History API,您将遇到一些问题,每个 HTML5 浏览器处理 API 的方式都略有不同,因此您不能只编写一次代码并期望它在不实现解决方法的情况下工作。 History.js 为 HTML5 History API 提供跨浏览器 API 和可选的 hashchange 后备对于 HTML4 浏览器,如果您想走这条路。

要将您的网站升级为 RIA/Ajax 应用程序,您可以使用以下代码片段:
https://github.com/browserstate/ajaxify

这是较长文章的一部分 智能状态处理,其中介绍了有关 hashbang、哈希和 html5 历史 api 的说明。

如果您需要任何进一步的帮助,请告诉我:)干杯。

Using the native HTML5 History API you're going to run into some problems, every HTML5 browser handles the API a little bit differently so you can't just code it once and expect it to work without implementing workarounds. History.js provides a cross-browser API for the HTML5 History API and a optional hashchange fallback for HTML4 browsers if you want to go down that route.

For upgrading your website into a RIA/Ajax-Application you can use this code snippet:
https://github.com/browserstate/ajaxify

Which is part of the longer article Intelligent State Handling which goes into explanations about hashbang, hashes and the html5 history api.

Let me know if you need any further help :) Cheers.

漆黑的白昼 2024-10-27 09:53:43

您需要获取 event.originalEvent

// Somewhere in your previous code
if (history && history.pushState) {
  history.pushState({module:"leave"}, document.title, this.href);
}


$(window).bind("popstate", function(evt) {
  var state = evt.originalEvent.state;
  if (state && state.module === "leave") {
    $.getScript(location.href);
  }
});

You need to get the event.originalEvent

// Somewhere in your previous code
if (history && history.pushState) {
  history.pushState({module:"leave"}, document.title, this.href);
}


$(window).bind("popstate", function(evt) {
  var state = evt.originalEvent.state;
  if (state && state.module === "leave") {
    $.getScript(location.href);
  }
});
杀お生予夺 2024-10-27 09:53:43

当 popstate 事件在页面加载时触发时,它的事件对象中不会有 state 属性。这允许您检查是否为页面加载触发事件。

window.onpopstate = function (event) {
  if (event.state) {
    // do your thing
  } else {
    // triggered by a page load
  }
}

When the popstate event is fired on page load it will not have a state property in the event object. This allows you to check if the event is fired for a page load or not.

window.onpopstate = function (event) {
  if (event.state) {
    // do your thing
  } else {
    // triggered by a page load
  }
}
如歌彻婉言 2024-10-27 09:53:43

当浏览器首次加载时,它总是会触发 popstate 事件。所以你需要确定这个popstate是否是你的。

当你执行pushState时,确保你有一个状态对象。这样您以后就可以检查一下。

然后在 popstate 处理程序上,检查状态对象 :)

$(function() {
    $(window).bind("popstate", function(data) {
        if (data.state.isMine)
            $.getScript(location.href); 
    });
});

// then add the state object
history.pushState({isMine:true},title,url);

如果您需要更多帮助,请告诉我 :)

When the browser first loads, it always fires a popstate event. So you need to determine if this popstate is yours or not.

When you do your pushState, make sure you have a state object. That way you can check it later.

Then on the popstate handler, check the state object :)

$(function() {
    $(window).bind("popstate", function(data) {
        if (data.state.isMine)
            $.getScript(location.href); 
    });
});

// then add the state object
history.pushState({isMine:true},title,url);

Let me know if you need any more help :)

殤城〤 2024-10-27 09:53:43

我使用下面的方法来更改地址栏并保存当前状态(包括当前 HTML 正文),然后在单击后退按钮时重新加载它,而无需任何其他 AJAX 调用。所有内容都会保存在您的浏览器中:

<script type="text/javascript">
   $(document).ajaxComplete(function(ev, jqXHR, settings) {
      var stateObj = { url: settings.url, innerhtml: document.body.innerHTML };
      window.history.pushState(stateObj, settings.url, settings.url);
   });

   window.onpopstate = function (event) {
      var currentState = history.state;
      document.body.innerHTML = currentState.innerhtml;
   };
</script>

I use the below approach to change the address bar and save the current state including the current HTML body, and I reload it on the back button click without any other AJAX call. All gets saved in your browser:

<script type="text/javascript">
   $(document).ajaxComplete(function(ev, jqXHR, settings) {
      var stateObj = { url: settings.url, innerhtml: document.body.innerHTML };
      window.history.pushState(stateObj, settings.url, settings.url);
   });

   window.onpopstate = function (event) {
      var currentState = history.state;
      document.body.innerHTML = currentState.innerhtml;
   };
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文