views:

214

answers:

3

I'm able to get the 'src' attribute of an using .src. But I cannot get the contents that are located in another domain. Is this a browser security problem?

jQuery(document).ready(function() {
    var elementTest = $("iframe:first").get(0);
    alert(elementTest.src);  // ok
    alert(elementTest.innerHTML);  // not ok
});
+1  A: 

The property you are looking for is contentWindow.

Try this code:

alert(elementTest.contentWindow.document.innerHTML);

It is analogous to the current window object, but for the iframe. so you can also do this:

alert(elementTest.contentWindow.location.href);

Edit: Ah - I missed the fact that you have a page from another domain in the iframe. THis method won't work for you, because of the built-in browser restrictions for one domain (your parent window) and accessing information in another domain (your iframe).

Jeff Meatball Yang
This will only work if the content loaded in the iframe is from the same domain... in his case, this won't work.
TM
Permission denied to get property Window.document
cometta
look like in this case, i will able to tempt with contain in google gadget
cometta
+7  A: 

Yes, this restriction against inspecting content and executing code within that external domain is known as the same-origin policy.

altCognito
+1  A: 

What you are trying (in a sense) is cross-domain communication. This won't work in the normal way you've tried (due to the same-origin policy as noted by altCognito), but there are some solutions if you have control over the other server. Check out this article:

http://msdn.microsoft.com/en-us/library/bb735305.aspx

Peter