简体   繁体   中英

why $(window['iframeName'].document.body).html() doesn't work when i change .attr('src')

Why doesn't

$(window['iframeName'].document.body).html()

...work when i change .attr('src')

When i set the src attribute of iframe to any url when i creating the page, this code

$(window['iframeName'].document.body).html() 

...will work.

But when i change the src attribute by .attr('src',"www.google.com.sa") and wait to load the page,

$(window['iframeName'].document.body).html() 

...will doesn't work.

Where is the problem ?

尝试:

.attr('src',"http://www.google.com.sa")

A couple of things:

Firstly, setting attr('src') will create a new history item. You should consider if you actually want to do that, eg if someone can click the back button and get the last page. If you don't want the changed state in the history (desirable for ads or apps that use iframes instead of outright ajaex), then you would want to change the document's location instead.

Secondly, I find it much easier to interact with the page like this:

$("#frameID").contents().find("jquery selector");

You can get the full HTML using a call ilke this:

$("#frameID").contents(document).children().html()

When you load a page from a different domain in the iframe, you can't access it. This is for security reasons.

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