I have a page which contains an iframe. The iframe can take user's input (via button click or menu selection) and display content accordingly. What I need is, when a user manipulates the iframe, the browser push each set of iframe parameters into history; and when the user click at back button, the iframe will reload its content using the saved parameters from the previous history entry. I have a piece of code doing the reloading as shown below.
The strange thing is, when I make multiple settings on the iframe (hence multiple state entries added to history), and then click at back, it'll work like this,
Say that I have state 4, 3, 2 in history and I'm now at state 5
So after each click that successfully restore the state, it takes two clicks to trigger the popstate event (I tried statechanged event, too, with same result) and restore to another previous state.
Anyone know what's going on here? Thanks in advance.
History.Adapter.bind(window, "popstate",
function (event) {
console.log("----state changed------", History.getState());
console.log("----state data------",History.getState().data.state);
//code to do reload an iframe to its proper state
});
So I stumbled on this same issue. The situation we had was using history.pushState()
to update the URL and then after that changing the src
attribute of an <iframe>
. By changing the src
of the <iframe>
, we are implicitly telling the browser to add to its history that the iframe had changed. Thus...
window.history
's popstate is
triggered on the <iframe>
's src attribute and is reverted back to it's previous state. popstate
event for the state you originally pushed onto the stackOur solution involved that when we decided to update the URL and push the state onto the stack, when we wanted to update the <iframe>
, we used jQuery to replace the <iframe>
list so...
$("#iFrameID").replaceWith(('<iframe id="iFrameID" src="' + location + '"></iframe>'
By doing this, we removed the side effect of the browser history getting polluted with our update to the <iframe>
tag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With