【问题标题】:Refreshing iframe error刷新 iframe 错误
【发布时间】:2016-05-17 17:48:19
【问题描述】:

我在 div 块中使用 iframe,并尝试通过使用以下代码单击按钮来更改其 src。

function helloClick(){
            var iframe = document.getElementById("iframeDiv");
            iframe.src = "hello_world";
            iframe.contentDocument.location.reload(true);
}

hello_world 文件与定义 iframe 的文件位于同一文件夹中。每次单击按钮时,我都会收到以下错误消息。

Uncaught TypeError: Cannot read property 'location' of undefined

我该如何解决这个问题?

【问题讨论】:

  • 如果 iframe 位于另一个域中,您将无法访问 contentDocument,因此您会收到该错误。它是否在另一个域而不是它显示的页面上?
  • @charlietfl 不,它不在另一个域上。我正在本地服务器上构建一个测试网站,并且 iframe 文件和 src 文件位于同一位置。
  • 使用 onload 处理程序并在其中尝试
  • 您的文件是否有“hello_world.php”或“hello_world.c”等扩展名?
  • 哦!它是一个基本的 html 文件。我没有在上面添加任何扩展,但对于浏览器来说没问题。

标签: javascript html iframe reload


【解决方案1】:

试试下面的代码sn-p

function helloClick(){
            var iframe = document.getElementById("iframeDiv");
            iframe.src = "hello_world";
            iframe.src = iframe.src;
}

它应该对你有帮助。

【讨论】:

  • 非常欢迎 :) 如果对您有用,您可以接受答案。
猜你喜欢
  • 2012-08-20
  • 2011-03-15
  • 1970-01-01
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 2012-08-13
相关资源
最近更新 更多