简体   繁体   English

Javascript:在新页面中打开链接并运行脚本

[英]Javascript: Open link in new page and run script

I require the ability to open a new window 我需要能够打开一个新窗口

window.open(url,'_blank');

Then run a javascript script such as 然后运行一个javascript脚本,如

window.open(url,'_blank').ready("javascript in here");

But I don't know how to do it, is there a way that I can do this? 但我不知道怎么做,有没有办法可以做到这一点?


The description is short but I think that's all that needs to be said 描述很简短,但我认为这就是需要说的

In short you cannot do what you are asking. 总之,你无法做你想要的。 The new window is sandboxed. 新窗口是沙盒。 It can only run javascript referenced within its own html file. 它只能运行在自己的html文件中引用的javascript。

One possible solution that might get you what you need would be to use postMessage. 一个可能为您提供所需功能的解决方案是使用postMessage。 Even so in order to do this the receiving page needs to be listening to potential messages. 即便如此,为了做到这一点,接收页面需要收听潜在的消息。 https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

An alternative would be to include a query param in the url and parse that param out client side in the new window and take action based on the value of the param. 另一种方法是在URL中包含一个查询参数,并在新窗口中解析出客户端,并根据参数的值采取行动。

I don't know exactly what your use case is, but browser extensions allow you to run arbitrary JavaScript code in webpages ( content_scripts for Chrome, for example). 我不确切知道您的用例是什么,但浏览器扩展允许您在网页中运行任意JavaScript代码(例如,Chrome的content_scripts )。

The downside is that the user need to install the extension. 缺点是用户需要安装扩展。

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

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