绑定窗口到 popstate 事件触发两次

发布于 2025-01-04 05:20:10 字数 348 浏览 1 评论 0原文

只需将窗口(在 jQuery 中)绑定到 popstate,该事件总是会触发两次。

$( window ).bind( 'popstate', myFunction );

myFunction() 中没有任何东西会导致这种情况 - 我尝试将此函数剥离为简单的:

console.log( 'triggered' );

结果是相同的。它总是触发两次(在 Safari、Chrome 和 Firefox 中测试)。

我知道 HTML5 历史记录 API 存在问题,但除了“尝试 History.js”之外的任何建议,我们将不胜感激!

Simply binding the window (in jQuery) to popstate, the event is always triggered twice.

$( window ).bind( 'popstate', myFunction );

There's nothing in myFunction() to cause this - I've tried stripping out this function to a simple:

console.log( 'triggered' );

And the result is the same. It's always triggered twice (tested in Safari, Chrome & Firefox).

I know there are issues with the HTML5 history API, but any advice other than 'try History.js' would be gratefully received!

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

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

发布评论

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

评论(4

写下不归期 2025-01-11 05:20:10

好吧,我想出了一个办法来解决这个问题:

jQuery( function( 
nbsp;){

  var currentPageNo = location.hash || 1;

  var myFunction = function(){

    var pageNo = location.hash;

    if( pageNo != currentPageNo ){

      // trigger AJAX stuff here

      currentPageNo = pageNo;

    }

  };

  $( window ).bind( 'popstate', myFunction );

});

只触发一次函数,但状态仍然触发两次!

Ok, I figured out a way to solve this:

jQuery( function( $ ){

  var currentPageNo = location.hash || 1;

  var myFunction = function(){

    var pageNo = location.hash;

    if( pageNo != currentPageNo ){

      // trigger AJAX stuff here

      currentPageNo = pageNo;

    }

  };

  $( window ).bind( 'popstate', myFunction );

});

Only triggers the function once, but the state is still triggered twice!

撩动你心 2025-01-11 05:20:10

这并没有直接回答这个问题,但可能对发现这个问题的其他人有帮助。因为我只需要监听一次popstate,然后立即删除监听器。

如果想在监听器触发后(仅一次)将其释放,可以将以下对象添加到 addEventListener 的第三个参数中:

window.addEventListener('popstate', myFunction, {once: true});

一次

一个布尔值,指示添加后最多调用一次侦听器。如果为 true,则调用时侦听器将自动删除。

这是文档: https://developer.mozilla.org /en-US/docs/Web/API/EventTarget/addEventListener

This does not answer the question directly, but it might be of help to others who find this question. Because I only needed to listen for the popstate once, and remove the listener immediately.

If you want to dispose of the listener after it has been triggered (only once), you can add the following object to the third parameter of addEventListener:

window.addEventListener('popstate', myFunction, {once: true});

once

A Boolean indicating that the listener should be invoked at most once after being added. If true, the listener would be automatically removed when invoked.

Here is the documentation: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

转角预定愛 2025-01-11 05:20:10

使用history API时,popstate事件被触发两次,即histry.back()和history.go(-1)。但仅使用浏览器后退按钮一次。

我使用超时作为解决方法:

var timeout = null;
$(window).bind('popstate', function(event) {
    clearTimeout(timeout);
    timeout = setTimeout(function() {
        console.log( 'triggered' );
    }, 50);
});

the popstate event is triggered twice when using the history API i.e. histry.back() and history.go(-1). but just once using the browser back button.

I use a timeout as a workaround:

var timeout = null;
$(window).bind('popstate', function(event) {
    clearTimeout(timeout);
    timeout = setTimeout(function() {
        console.log( 'triggered' );
    }, 50);
});
沉鱼一梦 2025-01-11 05:20:10

我在处理 ReactJS 中的后退按钮时遇到以下问题

  1. 第一个 popstate 事件第一次没有被调用
  2. 它在执行我的后退按钮自定义逻辑后被调用两次

以下代码

  componentDidMount() {
    window.history.pushState(null, null, window.location.pathname);
    window.addEventListener('popstate', this.onBackButtonEvent);
  }

以解决问题 1 我执行了 解决问题2 我在下面的代码中做了,

   onBackButtonEvent = (e) => {
    e.preventDefault();
    if (!this.isBackButtonClicked) {

  if (window.confirm("Do you want to save your changes")) {
       this.isBackButtonClicked = true;
       // your custom logic to page transition
        } else {
         window.history.pushState(null, null, window.location.pathname);
         this.isBackButtonClicked = false;
       }
    }
  }

不要忘记在构造函数中添加 this.isBackButtonClicked = false; 并取消事件。

  componentWillUnmount = () => {
    window.removeEventListener('popstate', this.onBackButtonEvent);
  }

I having below problems when handling the back button in reactjs

  1. First popstate event is not being called at the first time
  2. It is called twice after executing my back button custom logic

to Solve problem 1 I did the following code

  componentDidMount() {
    window.history.pushState(null, null, window.location.pathname);
    window.addEventListener('popstate', this.onBackButtonEvent);
  }

to solve problem 2 I did below code,

   onBackButtonEvent = (e) => {
    e.preventDefault();
    if (!this.isBackButtonClicked) {

  if (window.confirm("Do you want to save your changes")) {
       this.isBackButtonClicked = true;
       // your custom logic to page transition
        } else {
         window.history.pushState(null, null, window.location.pathname);
         this.isBackButtonClicked = false;
       }
    }
  }

don't forget to add this.isBackButtonClicked = false; in constructor and unscubscribe the events.

  componentWillUnmount = () => {
    window.removeEventListener('popstate', this.onBackButtonEvent);
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文