开发者

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

Why doesn't

$(window['ifra开发者_Python百科meName'].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 ?


try:

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


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


A couple of things:

Firstly, setting attr('src') will create a new history item. You should consider if you actually want to do that, e.g. 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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜