简体   繁体   English

在另一页上调用Javascript函数

[英]Call Javascript Function on another page

I am trying to click a link on another page(I do not have access to that page) which loads a JS function. 我试图单击另一个页面(我无权访问该页面)上的链接,该链接加载了JS函数。

So can I call that function from my page thru a href="" or thru onClick="" 因此,我可以通过href =“”onClick =“”从页面中调用该函数吗?

If it's on a different domain and you can't change its content, then you can't do it due to the same origin policy . 如果它在不同的域上并且您不能更改其内容,则由于相同的原始策略 ,您将无法执行。 Sorry. 抱歉。 You'll have to find a different way to achieve whatever you're trying to do. 您将必须找到一种不同的方式来实现您想要做的任何事情。

You can't call a function remotely from another page. 您不能从另一个页面远程调用函数。 Besides, you already left your page by then. 此外,到那时您已经离开了页面。

Unless... 除非...

  • that other page is somehow loaded to your page (AJAX+CORS or AJAX+proxy server maybe) and it's contents parsed on your page . 另一页以某种方式加载到您的页面(可能是AJAX + CORS或AJAX + proxy服务器),并且其内容已在页面上解析。

  • that other page has some logic that reads hashtags or query strings and executes corresponding functions. 另一个页面具有读取标签或查询字符串并执行相应功能的逻辑。 This could work inter-domain via iframes, as you are indirectly controlling the iframe via it's hash/query string. 可以通过iframe跨域工作,因为您是通过其哈希/查询字符串间接控制iframe的。

  • that other page could be loaded via iframes. 其他网页可以通过iframe加载。 iframes would work if "I do not have access to that page" meant that you just don't have access on the current subdomain but both live on the same top domain . 如果“我无权访问该页面”意味着您仅在当前子域上没有访问权限,而两者都位于同一个顶级域上,则iframe会起作用。

    Subdomain iframe communications can work . 子域iframe通信可以正常工作 Pages on example.com and subdomain.example.com can talk to each other but example1.com and example2.com can't talk to each other because both live on different domains. example.comsubdomain.example.com上的页面可以互相通信,但是example1.comexample2.com不能相互通信,因为它们都位于不同的域中。 See the other answer regarding the Same Origin Policy. 请参阅有关“相同来源策略”的其他答案。

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

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