如何获取iframe的当前位置? [英] How do I get the current location of an iframe?

查看:162
本文介绍了如何获取iframe的当前位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个基本的数据录入应用程序,允许用户浏览iframe中的外部内容,并从同一页面快速输入数据。其中一个数据变量是URL。
理想情况下,我希望能够使用javascript将iframe当前URL加载到文本框中。我现在意识到,由于安全问题,这不会发生。

I have built a basic data entry application allowing users to browse external content in iframe and enter data quickly from the same page. One of the data variables is the URL. Ideally I would like to be able to load the iframes current url into a textbox with javascript. I realize now that this is not going to happen due to security issues.

有没有人在服务器端做任何事情?或知道浏览器控件中的任何.Net浏览器。最终的目标是让用户轻松地提取在iframe中查看的页面的url它不一定是一个iframe,浏览器中的浏览器将是理想的。

Has anyone done anything on the server side? or know of any .Net browser in browser controls. The ultimate goal is to just give the user an easy method of extracting the url of the page they are viewing in the iframe It doesn't necessarily HAVE to be an iframe, a browser in the browser would be ideal.

谢谢,
Adam

Thanks, Adam

推荐答案

我在Firefox 3中做了一些测试比较在 iframe .src .documentWindow.location.href 的值C $ C>。 (注意:Chrome中的 documentWindow 被称为 contentDocument ,而不是 .documentWindow.location .href 在Chrome中将是 .contentDocument.location.href 。)

I did some tests in Firefox 3 comparing the value of .src and .documentWindow.location.href in an iframe. (Note: The documentWindow is called contentDocument in Chrome, so instead of .documentWindow.location.href in Chrome it will be .contentDocument.location.href.)

src 始终是没有用户交互的iframe中加载的最后一个URL。也就是说,它包含URL的第一个值,或者是从包含的窗口中用Javascript设置的最后一个值:

src is always the last URL that was loaded in the iframe without user interaction. I.e., it contains the first value for the URL, or the last value you set up with Javascript from the containing window doing:

document.getElementById("myiframe").src = 'http://www.google.com/';

如果用户在iframe内导航,您不能再使用src访问URL的值。在上一个示例中,如果用户离开www.google.com,并且执行以下操作:

If the user navigates inside the iframe, you can't anymore access the value of the URL using src. In the previous example, if the user goes away from www.google.com and you do:

alert(document.getElementById("myiframe").src);

您仍然会收到 http://www.google.com

documentWindow.location.href 仅在iframe包含与包含窗口相同的域中的页面时可用,但如果可用,则始终包含该URL的正确值,即使用户在iframe中导航。

documentWindow.location.href is only available if the iframe contains a page in the same domain as the containing window, but if it's available it always contains the right value for the URL, even if the user navigates in the iframe.

如果您尝试访问 documentWindow.location.href (或 documentWindow 下的任何内容),并且iframe在不属于包含窗口的域的页面中,将引发异常:

If you try to access documentWindow.location.href (or anything under documentWindow) and the iframe is in a page that doesn't belong to the domain of the containing window, it will raise an exception:

document.getElementById("myiframe").src = 'http://www.google.com/';
alert(document.getElementById("myiframe").documentWindow.location.href);
Error: Permission denied to get property Location.href

我还没有测试任何其他浏览器

I have not tested any other browser.

希望有帮助!

这篇关于如何获取iframe的当前位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆