简体   繁体   中英

Extract IFrame source using JavaScript

I have a simple HTML/PHP web page and one IFrame in it. The IFrame contains links to external web sites.

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). How can this be done?

You can use .contents() to get the contents of an iFrame. That page has an example too.

The following line will give you the current URL of the iFrame. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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