views:

44

answers:

3

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 ?

+1  A: 

try:

.attr('src',"http://www.google.com.sa")
Vaibhav Gupta
this is not my question.there is no any problem in url.why $(window['iframeName'].document.body).html() doesn't work
faressoft
A: 

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()
dmrnj
$("#frameID").contents(document).children().html() it is like my code and it doesn't work after i change src by .attr('src',"www.google.com.sa")
faressoft
It sounds like the reason this is happening is because of cross-domain rules. When your document lives on a different domain (or subdomain) than the iframe, then your page can't access or interact with the other page. It's built-in browser security. http://en.wikipedia.org/wiki/Same_origin_policyYou can probably verify this is happening by looking in your browser's error log or trying to execute the jquery from a javascript console. If should tell you permission denied or some such error.
dmrnj
A: 

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

Guffa
I don't think than ! when i set src from the orginal code of the page the, iframe will load the site (src).
faressoft
@faressoft: I have tried this both in Firefox 3.6.3 and Internet Explorer 8, and they give the error "access denied" when trying to access a page from a different domain, regardless of whether the iframe source is set in the original HTML code or using Javascript. Some browsers may allow some cross domain access, but most browsers don't.
Guffa
:0take a lookhttp://mkb.ma/as
faressoft
@faressoft: I can't find any code like that on that page.
Guffa
Iam sorry, you are right.
faressoft