How do I call a function of another window in javascript?

Opening a new tab using window.open. The new tab has a javascript function myfunction

defined in the script tag. From a window that opens a new window, I want to run this function.

How can i do this?

thank

Something like

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

      

EDIT

This does not work. It doesn't make a warning.

knife

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

      

new inset

        function receiveMessage(event) {
            alert(event.data);
        }
        window.addEventListener("message", receiveMessage, false);

      

+3


source to share


2 answers


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

a.addEventListener('load', function(){
  a.myfunction("hi");
}, true);

      



+5


source


Assuming the php file provides the content for the new window:

window.open('http://www.example.com/myFile.php?parameter=' + parameter, 'myWindow', 'width=500,height=500,scrollbars=yes').focus();

      

Then in the php section myFile.php:

$myParam = (isset($_GET['parameter'])) ? $_GET['parameter'] : null;

      



Then, using the body onload, call the function:

<head>
<script>
    var myParam = '<?= $myParam ?>';
</script>
</head>
<body onload="myFunction()">

      

The function checks for the existence of $ myParam:

function myFunction() {
    if (myParam) {
        //do Something
    }
}

      

0


source







All Articles