简体   繁体   English

如何在不重新加载 iframe 的情况下更改 iframe src?

[英]How to change iframe src without reloading the iframe?

I have two frames of which I copy the content of the first ( myframe ) to the second ( myframe2 ) using JavaScript and set myframe2 's src:我有两个框架,我使用 JavaScript 将第一个 ( myframe ) 的内容复制到第二个 ( myframe2 ) 并设置myframe2的 src:

<iframe id="myframe" src="../Page1.aspx" width="100%" height="100px"></iframe>
<iframe id="myframe2"  width="100%" height="100px"></iframe>

<button onclick="myFunction();return false;">Try it</button>

<script>
    function myFunction() {
        var iframe1 = document.getElementById("myframe");
        var iframe2 = document.getElementById("myframe2");

        iframe2.contentWindow.document.body.parentElement.innerHTML = iframe1.contentWindow.document.body.parentElement.innerHTML;
        iframe2.setAttribute("src", iframe1.src);
    }
</script>

FrameSecond reloads after "src" attribute is set, but I don't need it to do so. FrameSecond在“src”属性设置后重新加载,但我不需要它这样做。 Can anyone help me with this?任何人都可以帮我解决这个问题吗?

使用replaceState(state, title, URL)

iframe2.contentWindow.history.replaceState('WhatEverNameYouWant', "", iframe1.src);

I had much the same problem.我有很多同样的问题。 I needed to "know" what the location of the document loaded into my Iframe was, so I could show its location somewhere else.我需要“知道”加载到 Iframe 中的文档的位置,以便我可以在其他地方显示它的位置。 I had inputs which changed the location of the iframe by setting我有通过设置更改 iframe 位置的输入

myIFrame.src =  ... 

This worked fine except then I realized when I clicked back- or forward -button the content of the IFrame changed, yet that did not (seem to) change the value of the src-attribute of the referring IFrame element.这工作得很好,但当我单击后退或前进按钮时,我意识到 IFrame 的内容发生了变化,但这并没有(似乎)改变了引用 IFrame 元素的 src-attribute 的值。 Same problem when I added links into the iframe documents to navigate between them.当我将链接添加到 iframe 文档以在它们之间导航时出现同样的问题。

My solutions was to in the onload-handler of the document loaded into the IFrame record the loaded URL into a field of the parent window.我的解决方案是在加载到 IFrame 的文档的 onload-handler 中将加载的 URL 记录到父窗口的字段中。 When the containing document needs to know what is loaded in the IFrame it consults that variable rather than rely on the .src -attribute of its IFrame -element, which seems to not be up-to-date with the actual contents of the iframe automatically.当包含文档需要知道 IFrame 中加载的内容时,它会参考该变量而不是依赖其 IFrame 元素的 .src 属性,这似乎与 iframe 的实际内容自动保持同步.

BTW.顺便说一句。 I also tried explicitly setting the value of the .src -attribute of the iframe elemen from the onload -handler of the iframed document.我还尝试从 iframe 文档的 onload 处理程序中明确设置 iframe 元素的 .src 属性的值。 That worked yes but caused a visible second load of the iframe contents.那是有效的,但导致了 iframe 内容的第二次可见加载。 By using a separate explicit location to store the url of the document loaded into the iframe it does not get loaded twice, and everything works smoothly.通过使用单独的显式位置来存储加载到 iframe 中的文档的 url,它不会被加载两次,并且一切正常。

Not really an answer to this question specifically, but maybe it's useful for someone.不是这个问题的真正答案,但也许它对某人有用。

I needed to pass the language to the Iframe, what worked for me was adding the parameter to the hash instead of the query string.我需要将语言传递给 Iframe,对我有用的是将参数添加到哈希而不是查询字符串。 because a hash change doesn't trigger a page reload in the Iframe:因为哈希更改不会触发 Iframe 中的页面重新加载:

eg ' http://...?uiCulture=en ' --> ' http://...#uiCulture=en '例如' http://...?uiCulture=en '-->' http://...#uiCulture=en '

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM