Call a JavaScript function across browser tabs

前端 未结 4 2013
离开以前
离开以前 2020-12-10 17:37

I have two browser tabs, tab1 and tab2.

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

Is that possible and if s

4条回答
  •  情歌与酒
    2020-12-10 18:09

    JavaScript can not do cross-tab scripting in the browser (it is a security risk).

    If however the 2nd tab was opened from a window.open() call, and the browsers settings were set up such that new popup windows open in a new tab instead -- then yes, "tab1" can talk to "tab2"

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

    opener.doSomething();
    

    likewise, the opener can call functions on the new tab/popup, by using the variable it created when creating the popup window.

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

提交回复
热议问题