3

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:

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;
});

i.e.

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?

2 Answers2

4

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... :-)

  • I'm not using JQuery and have the same problem. I can't put a funciton as third parameter, how did you solve the problem? – Lancelot Nov 11 '16 at 16:19
1

Here it says, jQuery has problems with capturing that popstate event and you should take the native onpopstate.

jQuery bind popstate event not passed.

However I wonder why it works once?

Community
  • 1
  • 1
loveNoHate
  • 1,489
  • 13
  • 21
  • My jQuery capturing works great, I found an answer, but can't answer my own question now (reputation to low) –  Jan 30 '14 at 00:20
  • @YaPKnn Ah yeah, finally got it too, changecontent gets called on popstate too yeah. – loveNoHate Jan 30 '14 at 02:26