简体   繁体   中英

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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