简体   繁体   English

使用JavaScript提取IFrame源

[英]Extract IFrame source using JavaScript

I have a simple HTML/PHP web page and one IFrame in it. 我有一个简单的HTML / PHP网页和一个IFrame。 The IFrame contains links to external web sites. IFrame包含指向外部网站的链接。

I need some JavaScript that will extract the exact URL of the web page that is currently displayed inside the IFrame (not just the base URL of the external site). 我需要一些JavaScript来提取IFrame内部当前显示的网页的确切URL(而不仅仅是外部网站的基本URL)。 How can this be done? 如何才能做到这一点?

You can use .contents() to get the contents of an iFrame. 您可以使用.contents()来获取iFrame的内容。 That page has an example too. 该页面也有一个示例。

The following line will give you the current URL of the iFrame. 以下行将为您提供iFrame的当前 URL。 Not the initial, but whatever it is showing when you call this: 不是首字母缩写,而是调用它时显示的内容:

 document.getElementById("iframe").contentWindow.location.href

Note: This will only work if the page is on the same domain. 注意:仅当页面在同一域中时,这才有效。

The way to access the url on the iframe is by checking the src attribute. 在iframe上访问url的方法是检查src属性。

console.log(iframe.src);
// will print http://example.com

However, when the user navigate to a different page in the iframe, the parent window will still show the original url on the src property 但是,当用户导航到iframe中的其他页面时,父窗口仍会在src属性上显示原始网址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM