重新加载 IFRAME 而不添加到历史记录
我正在更改 IFRAME 的 src
以便重新加载它,它工作正常,并在加载 HTML 时触发 onload
事件。
但它在历史中添加了一个条目,这是我不想要的。 有什么方法可以重新加载 IFRAME 并且不影响历史记录吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用replace()只是您自己的域iframe的一个选项。 它无法在远程站点(例如:twitter 按钮)上工作,并且需要一些特定于浏览器的知识才能可靠地访问子窗口。
相反,只需删除 iframe 元素并在同一位置构造一个新元素即可。 仅当您在 DOM 中修改 src 属性后才会创建历史记录项,因此请确保在追加之前设置它。
编辑:JDandChips 正确地提到您可以从 DOM 中删除、修改和重新附加。 不需要构建新鲜的。
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.
您可以使用 javascript
location.replace
:You can use javascript
location.replace
:就像 Greg 上面所说的那样,.replace() 函数就是如何做到这一点的。 我似乎不知道如何回复他的答案*,但诀窍是引用 iFrames contentWindow 属性。
*刚刚得知我需要更多声誉才能对答案发表评论。
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.
重新创建 iframe 的另一种方法是从 DOM 中删除 iframe,更改 src,然后重新添加它。
在很多方面,这与
replace()
建议类似,但当我尝试使用 History.js 尝试这种方法并手动管理状态时,我遇到了一些问题。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.一种解决方案是使用
object
标记而不是iframe
标记。替换此:
通过此:
将允许您更新
data
属性而不影响历史记录。 如果您使用声明式框架(例如React.js),并且您不应该执行任何命令式命令来更新 DOM,那么这非常有用。有关
iframe
和object
之间差异的更多详细信息:使用 Iframe 或对象标记将网页嵌入到另一个页面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尝试使用此功能将旧 iframe 替换为从旧 iframe 复制的新 iframe:
像这样使用它:
这种方式不会将 iframe 的 URL 添加到浏览器历史记录中。
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.
JDandChips 答案在跨源 iframe (youtube) 上为我工作,这里是 vanilla JS (没有 JQuery):
JDandChips answer worked for me on a cross origin iframe (youtube), here it is in vanilla JS (without JQuery):
使用
replace
方法绕过将 iframe 的 URL 添加到历史记录中:HTML:
JavaScript:
Use the
replace
method to bypass the addition of the iframe's URL to history:HTML:
JavaScript:
@JDandChips 上面有一个很好的答案,但语法应该从 parent() 更新为 parentElement:
@JDandChips has a great answer above, but the syntax should be updated from parent() to parentElement:
最简单、快速的加载解决方案
使用
window.location.replace
在加载页面或框架时不更新历史记录。对于链接,它看起来像这样:
或
但是如果您希望它不从链接执行而是在加载框架时自动执行,那么您应该将其放在 iframe 文件正文部分中:
如果由于某种原因 onload 不执行不在 iframe 中加载,然后将目标框架名称而不是窗口放在语法中,如下所示:
注意:对于此脚本,所有
onclick
、onmouseover
、onmouseout
、onload
和 < code>href="javascript:" 可以工作。The most simple and fast loading solution
Use
window.location.replace
to not update the history when loading the page or the frame.For links it looks like this:
or
But if you want it not to act from link but to act automatically when loading the frame then from the iframe you should put this in the iframe file body section:
If for some reason the onload does not load in the iframe then put your target frame name instead of window in the syntax like this:
NOTE: For this script all
onclick
,onmouseover
,onmouseout
,onload
andhref="javascript:"
will work.还有一个很好的解决方案,通过设置 iframe 的 key 属性来防止 iframe 添加到历史记录。
特别是如果使用像...
react
vue
这样的框架,可以在这里找到非常详细的说明:
https://www.aleksandrhovhannisyan.com/blog/react-iframes-back-navigation-bug/
There is also a nice solutions by setting the
key
attribute of theiframe
to prevent the iframe from adding to the history.Especially if working with frameworks like...
react
vue
A very detailed explentation can be found here:
https://www.aleksandrhovhannisyan.com/blog/react-iframes-back-navigation-bug/