javascript 在 html 页面的 iframe 内显示警告框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12139953/
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
displaying an alert box inside an iframe in an html page
提问by qre0ct
There is a simple HTML page, named say abc.html. Now abc.html has a button named say 'click me'. This page, abc.html, also has an iframe, with an id say 'myframe'.
有一个简单的 HTML 页面,名为 say abc.html。现在 abc.html 有一个名为“点击我”的按钮。这个页面 abc.html 也有一个 iframe,id 为“myframe”。
Now what i want is that when the button, 'click me', is clicked an alert box should come up inside the iframe named 'myframe'. How can i do this?
现在我想要的是,当单击“单击我”按钮时,应该在名为“myframe”的 iframe 中出现一个警告框。我怎样才能做到这一点?
I have tried the following..
我试过以下..
window.frames[0].contentWindow.alert("this is the iframe");
but this does not seem to work. Please let me knwo why does this not work and what could be an alternate solution.
但这似乎不起作用。请让我知道为什么这不起作用以及什么可能是替代解决方案。
采纳答案by jett
ContentWindow is derived from document, not window.
ContentWindow 派生自文档,而不是窗口。
What you will want to do is add the alert into a function on the iframe page, then use document.getElementById('targetFrame').contentWindow.targetFunction();
to call it, (using targetFunction as example). This answermay give you further information on techniques.
您要做的是将警报添加到 iframe 页面上的函数中,然后使用document.getElementById('targetFrame').contentWindow.targetFunction();
它来调用它(以 targetFunction 为例)。这个答案可能会为您提供有关技术的更多信息。
回答by freefaller
If the source of the <iframe>
comes from a different domain than the parent page, the you are going to hit browsers same origin policy, meaning that you simply cannot do anything with that page.
如果 的来源<iframe>
来自与父页面不同的域,则您将使用浏览器同源策略,这意味着您根本无法对该页面执行任何操作。
For an "alternative suggestion" it really does depend on exactly what you are trying to achieve, and what the user is supposed to experience.
对于“替代建议”,它确实取决于您想要实现的目标以及用户应该体验的内容。
回答by Ryan Kinal
Browser-level alert
is not visibly tied to a particular frame. You will alwaysget an alert that is centered on the browser window, rather than centered on the window/frame it is called from.
浏览器级别alert
并不明显地绑定到特定的框架。您将始终收到以浏览器窗口为中心的警报,而不是以调用它的窗口/框架为中心。
If you need this functionality, you'll have to use your own modal dialogues, or a library that will present them.
如果您需要此功能,则必须使用您自己的模态对话,或将呈现它们的库。
回答by Nikhil D
This might helpful to you:
这可能对您有帮助:
function body_load()
{
document.getElementById('loader').src;
}
<div id='mydiv'>
<input type="button" value="Click Me" onclick="body_load();" />
</div>
<iframe id='loader' onload="body_load(this)" name="Google" scrolling="auto" style="width:95%;height:600px" src="http://www.image.com"></iframe>