1

我已经从这个网站收到了 javascript 代码。

alert(window.frames['sc_frame'].document.getElementsByTagName('body')[0].innerHTML);

或者

alert(window.frames['sc_frame'].document.getElementsByTagName('body')[0].innerText);

问题是我有这个错误。

Unsafe JavaScript attempt to access frame with URL http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxx from frame with URL http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxx. Domains, protocols and ports must match.
(anonymous function)
InjectedScript._evaluateOn
InjectedScript._evaluateAndWrap
InjectedScript.evaluate
TypeError: Cannot call method 'getElementsByTagName' of undefined
arguments: Array[2]
get message: function () { [native code] }
get stack: function () { [native code] }
set message: function () { [native code] }
set stack: function () { [native code] }
type: "non_object_property_call"
__proto__: Error

我也理解原因,因为我IFrame正在调用另一个域。

<iframe id="sc_frame" src="http://example1.com"></iframe>

我使用上面的代码http://example.com

我的问题是是否有解决方法。使用javascript或任何其他方法?

4

1 回答 1

0

由于安全限制,您不能跨域(/端口/协议)直接与正文中的其他元素通信。您不仅不能访问目标文档源,也不能访问对象和事件。

可以做的是使用框架通过代理跨域发送消息。 这个答案谈到了这一点。

于 2012-12-26T14:05:48.563 回答