问题
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