Call JavaScript function on browser tabs

I have two browser tabs, tab1 and tab2.

I have a function called execute in tab1 that I would like to call from a page in tab2.

Is this possible, and if so, how?

+4
source share
4 answers

JavaScript cannot perform cross-site scripting in the browser (this is a security risk).

If, however, the second tab was opened from the call to window.open() , and the browser settings were configured so that new pop-up windows open on a new tab instead - then yes , "tab1" can talk to "tab2"

the first tab / window is called opener , and thus the new tab can call functions on the opener using this format:

 opener.doSomething(); 

likewise, a novice can call functions on a new tab / popup menu using the variable created when the popup was created.

 var myPopup = window.open(url, name, features); myPopup.doStuffOnPopup(); 
+16
source

You should show your html first, but I assume your tab has a link, you can do like this:

 <a href="some-path" id="tab2">Tab 2</a> 

JQuery

 $('#tab2').click(function(){ // your function code here // prevent default action return false; }) 
0
source

I really cannot understand what you mean by question.

But you can pass references to javascript functions as arguments to other functions. (Be careful with object execution contexts).

So, when initializing "tab2", you can send a link to the execution function "tab1" to "tab2".

Could give a more concrete answer using an example if you were more clear in your question.

0
source

A tiny open source component for synchronizing / blocking / calling code on multiple tabs that allows you to send messages between tabs (DISCLAIMER: I am one of the authors!)

https://github.com/jitbit/TabUtils

It can be called like this:

 TabUtils.BroadcastMessageToAllTabs("messageName", data); 

And then in another tab:

 TabUtils.OnBroadcastMessage("messageName", function (data) { //do something }); 

It is based on the onstorage event, you can just change the code for you, it is very simple.

0
source

Source: https://habr.com/ru/post/1306577/


All Articles