Javascript: Open link in new page and run script

廉价感情. 提交于 2020-01-14 09:04:58

问题


I require the ability to open a new window

window.open(url,'_blank');

Then run a javascript script such as

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


回答1:


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.

One possible solution that might get you what you need would be to use 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

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.




回答2:


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).

The downside is that the user need to install the extension.



来源:https://stackoverflow.com/questions/29306621/javascript-open-link-in-new-page-and-run-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!