繁体   English   中英

如何在另一个网页上调用js函数?

[英]How to call a js function on another webpage?

我想在另一个网页上调用javascript函数。 到目前为止,我的解决方案是创建一个包含包含目标页面的iframe的页面。

<a onclick="func1()">Call</a>
    <script type="text/javascript">
        function func1() 
        {
            document.getElementById('myFrame').contentWindow.some_function();
        }
    </script>
<iframe id="myFrame" src="http://www.webpage.com">

问题是,仅当两个页面具有相同的域时,此解决方案才有效。 否则,我得到这个错误:

Uncaught SecurityError: Blocked a frame with origin "http://my_webpage.com" from accessing a frame with origin "http://not_my_webpage.com". Protocols, domains, and ports must match. 

我已经搜索了几个小时,但是我只发现我需要使用

document.domain = document.domain;

使用它后,我得到这个错误:

Uncaught SecurityError: Blocked a frame with origin "http://my_webpage.com" from accessing a frame with origin "http://not_my_webpage.com". The frame requesting access set "document.domain" to "http://my_webpage.com", but the frame being accessed did not. Both must set "document.domain" to the same value to allow access. 

我想我需要在两个页面上都使用它,但是iframe中的页面不是我的,并且我没有访问权限。 有任何想法吗? 谢谢

你就是做不到。 想象一下,如果发生这种事情,那将是一个巨大的安全漏洞。 那是因为您无法访问具有其他原点的框架,浏览器会自动阻止每个试图访问具有不同原点的窗口的脚本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM