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。