Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 788
Zed Avatar asked Mar 27 '15 17:03

Zed


People also ask

How do I open a new page in JavaScript?

To open a new tab, we have to use _blank in the second parameter of the window. open() method. The return value of window. open() is a reference to the newly created window or tab or null if it failed.

How do I link one JavaScript page to another?

Answer: Use the JavaScript window. location Propertylocation property to make a page redirect, you don't need any jQuery for this. If you want to redirect the user from one page to another automatically, you can use the syntax window. location. replace("page_url") .

How do I open a link in a new tab but stay on the same page?

How do I open a link in a new tab but stay on the same page in Google Chrome? You can simply open a new tab and stay on the same page by: Right clicking the link that you want to open in a new tab. Then select, “Open link in new tab”


1 Answers

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.

like image 140
bhspencer Avatar answered Nov 05 '22 00:11

bhspencer