I'm changing an IFRAME's src
in order to reload it, its working fine and firing the onload
event when its HTML loads.
But it adds an entry to the history, which I don't want. Is there any way to reload an IFRAME and yet not affect the history?
I'm changing an IFRAME's src
in order to reload it, its working fine and firing the onload
event when its HTML loads.
But it adds an entry to the history, which I don't want. Is there any way to reload an IFRAME and yet not affect the history?
Try to use this function to replace old iframe with new iframe which is copied from old one:
Use it like this:
This way will not add URL of iframe to browser history.
Like Greg said above, the .replace() function is how to do this. I can't seem to figure out how to reply to his answer*, but the trick is to reference the iFrames contentWindow property.
*Just learned I need more reputation to comment on an answer.
You can use javascript
location.replace
:Using replace() is only an option with your own domain iframes. It fails to work on remote sites (eg: a twitter button) and requires some browser-specific knowledge to reliably access the child window.
Instead, just remove the iframe element and construct a new one in the same spot. History items are only created when you modify the
src
attribute after it is in the DOM, so make sure to set it before the append.Edit: JDandChips rightly mentions that you can remove from DOM, modifiy, and re-append. Constructing fresh is not required.
An alternative method to recreating the iframe would be to remove the iframe from the DOM, change the src and then re add it.
In many ways this is similar to the
replace()
suggestion, but I had some issues when I tried that approach with History.js and managing states manually.One solution is to use the
object
tag rather than theiframe
tag.Replacing this:
By this:
will allow you to update the
data
attribute without affecting the history. This is useful if you use a declarative framework such asReact.js
where you are not supposed to do any imperative command to update the DOM.More details on differences between
iframe
andobject
: Use of Iframe or Object tag to embed web pages in another