简体   繁体   English

后退按钮只能工作一次

[英]Back button works only once

I'm working on my page and can't find answer for my issue. 我在我的页面上工作,无法找到我的问题的答案。

I'm using window.history.pushstate, and onpopstate event, here is my code: 我正在使用window.history.pushstate和onpopstate事件,这是我的代码:

changeContent = function(href) {
  window.history.pushState({}, '', href);
  if (href == "/") {
    $('#main').load('main.html');
  } else {
    $('#main').html('<iframe id="gallery" src="photos'+href+'/index.html"></iframe>');
  }
};

$(window).on("popstate", function(e) {
  changeContent(location.pathname);
});

$(document).on("click", ".galleryLink", function() {
  var href = $( this ).attr( "href" );
  changeContent(href)
  return false;
});

ie

google page -> my main page -> gallery -> back button -> my main page -> back button -> my main page -> back button -> my main page 谷歌页面 - >我的主页 - >图库 - >后退按钮 - >我的主页 - >后退按钮 - >我的主页 - >后退按钮 - >我的主页

What's going on? 这是怎么回事?

Got it! 得到它了!

Line: 线:

window.history.pushState({}, '', href);

should be inside 应该在里面

$(document).on("click", ".galleryLink", function()

I put it inside 我把它放在里面

changeContent = function(href)

this was my fault... :-) 这是我的错...... :-)

Here it says, jQuery has problems with capturing that popstate event and you should take the native onpopstate . 在这里它说,jQuery捕获popstate事件有问题,你应该采取本机onpopstate

jQuery bind popstate event not passed . jQuery绑定popstate事件未通过

However I wonder why it works once? 但是我想知道为什么它一次有效?

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

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