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

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

问题描述

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

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.href IFRAME 。 (注: documentWindow 被称为 contentDocument 在Chrome这样的,而不是 .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的值。在previous例如,如果用户从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。

You will still get "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天全站免登陆