javascript 使用javascript重新加载iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15131897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:37:27  来源:igfitidea点击:

Reload iframe using javascript

javascriptjspiframe

提问by Rachel

  • I have a jsp page with iframe in it.
  • 我有一个带有 iframe 的 jsp 页面。

for example: worklist.jsp

例如:worklist.jsp

 <html>
 <head>
 <body>
 <iframe height="15%" marginheight="0px" marginwidth="0px" scrolling="no" src="menuBanner.jsp" style="border:0; padding: 0px; position:absolute;" width="960px">
</iframe>
<div><button data-dojo-type="dijit.form.Button" value="Reload" id="reload"  onclick="reload();">Reload</button></div>
</body></head></html>
  • On click of a button, i need to reload only iframe page.
  • I tried the below script, but its reloading the whole page(worklist.jsp)
  • 单击按钮后,我只需要重新加载 iframe 页面。
  • 我尝试了下面的脚本,但它重新加载了整个页面(worklist.jsp)

function reload(){ location.reload(); }

功能重新加载(){ location.reload(); }

回答by CharliePrynn

You can update/refresh the iframe by setting the src to the current src.

您可以通过将 src 设置为当前 src 来更新/刷新 iframe。

Here is an example.

这是一个例子。

var test = document.getElementById('iframeId');
test.src = iframe.src;

This way works for cross domain. The example in the comments are less hacky if iFrames are on the same domain.

这种方式适用于跨域。如果 iFrame 位于同一域中,则评论中的示例就不那么笨拙了。

回答by dirkk

function reload( {
  document.getElementById('iFrameID').contentDocument.location.reload(true);
}

回答by Behrad Farsi

you can try:

你可以试试:

var iframe = document.getElementById('YourIFrameId');
iframe.src = iframe.src + '?c=' + Math.random(); 

i think without changing the src attribute you will not be able to reload your iframe.

我认为如果不更改 src 属性,您将无法重新加载 iframe。

回答by Hari

You can do like this. Get the iframe src and assign again to the src of the iframe

你可以这样做。获取 iframe src 并再次赋值给 iframe 的 src

<script>
function test()
  {
    $('#subpage_frame').attr('src',$('#subpage_frame').attr('src')) ;
  }
</script>

<iframe src="test.com" onload="Loaded();" id="subpage_frame"></iframe>

<button onclick="test();">Refresh button</button>