Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function of another window in javascript? [duplicate]

I open a new tab using window.open. The new tab has a javascript myfunction function in it defined in a script tag. From the window that opens the new window, I want to run that function.

How can I do that?

Thanks

Something like

var a = window.open("mypage.html", "_blank");
a.myfunction("hi");

EDIT

This isn't working. Its not doing the alert.

opener

        var w = window.open("../scripts/map.php", "_blank");
        w.postMessage("The user is 'bob' and the password is 'secret'", "*");

new tab

        function receiveMessage(event) {
            alert(event.data);
        }
        window.addEventListener("message", receiveMessage, false);
like image 900
omega Avatar asked Aug 27 '14 01:08

omega


People also ask

How to call function from function in JavaScript?

The JavaScript call() Method The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

What is window open()?

The open() method opens a new browser window, or a new tab, depending on your browser settings and the parameter values.


1 Answers

var a = window.open("mypage.html", "_blank");
a.focus();

a.addEventListener('load', function(){
  a.myfunction("hi");
}, true);
like image 85
HDT Avatar answered Oct 20 '22 23:10

HDT