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?
我能够获得使用.src的'src'属性。但我无法获取位于另一个域中的内容。这是浏览器安全问题吗?
jQuery(document).ready(function() {
var elementTest = $("iframe:first").get(0);
alert(elementTest.src); // ok
alert(elementTest.innerHTML); // not ok
});
Yes, this restriction against inspecting content and executing code within that external domain is known as the same-origin policy.
是的,这种对检查内容和在该外部域中执行代码的限制称为同源策略。
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:
你在尝试(在某种意义上)是跨域通信。这不会以您尝试的正常方式工作(由于altCognito指出的同源策略),但如果您可以控制其他服务器,则有一些解决方案。看看这篇文章:
The property you are looking for is contentWindow.
您要查找的属性是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:
它类似于当前窗口对象,但对于iframe。所以你也可以这样做:
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).
编辑:啊 - 我错过了你有一个来自iframe中另一个域的页面的事实。这种方法不适合您,因为一个域(您的父窗口)的内置浏览器限制和访问另一个域(您的iframe)中的信息。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/24/f3e09653cb5fe58591c8d363f9e07257.html。