I'm having problems getting URL to update when hitting the back button on the browser (I'm on testing on Firefox). After updating the "src" property of the iframe I use replaceState
to update the history. If I hit the back button after this the iframe will go back to the previous page but the URL does not update to reflect this.
function updateURLBar(urlInfo) {
var stateObj = { foo: "bar" };
document.getElementById("iframeContent").src = urlInfo[1];
window.history.replaceState(stateObj, "page 2", urlInfo[0]);
}
Am I going about this the wrong way or am I just missing something. Thanks for any help in advanced!
You may find the popstate event interesting.
https://developer.mozilla.org/en-US/docs/Web/Events/popstate
First, I would change few lines of code that you have...
And then you can add:
EDIT
The Iframe caveat
There is a problem with using pushState and changing
iframe
src attributes. If aniframe
is in the DOM, and you change thesrc
attribute, this will add a state to the browsers history stack. Therefore, if you usehistory.pushState
withiframe.src = url
, then you will create 2 history entries.The Workaround
Changing the
src
attribute of aniframe
element when theiframe
is not in the DOM will not push to the browsers history stack.Therefore you could build a new
iframe
, set it'ssrc
attribute, and then replace the oldiframe
with the new one.