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

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

问题描述

我构建了一个基本的数据输入应用程序,允许用户在 iframe 中浏览外部内容并从同一页面快速输入数据.数据变量之一是 URL.理想情况下,我希望能够使用 javascript 将 iframes 当前 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.

谢谢,亚当

推荐答案

我在 Firefox 3 中做了一些测试,比较了 .src.documentWindow.location.hrefiframe 中.(注意:documentWindow 在 Chrome 中被称为 contentDocument,所以在 Chrome 中它不是 .documentWindow.location.href 而是 .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天全站免登陆