Here is my files
index.php
pwd.php
common.js
In the index.php
i am opening a file pwd.php
as mywindow and i wanted to close it by pressing a button in the My Window. How can i do this ?
index.php
<!DOCTYPE html>
<html>
<body>
<button onclick="openWin()">Open w3schools.com in a new window</button>
<?php
include ('common.js');
?>
</body>
</html>
'pwd.php'
<button onclick="closeWin()">Close the new window (w3schools.com)</button>
<?php
include ('common.js');
?>
common.js
<script>
var myWindow;
function openWin() {
myWindow = window.open("pwd.php", "_blank", "width=500, height=500");
}
function closeWin() {
myWindow.close();
}
</script>
When i try to close i am getting error Uncaught TypeError: Cannot read property 'close' of undefined
in the console of the pwd.php
.
What is the mistake i am doing an How can i achieve it ?
Note :
I don't want to call the closeWin()
from the Parent window.
I want to call the closeWin()
in any even from the myWindow that is created.
Use window.parent.closeWin(); in close button because its inside the iframe and your function is in your parent window.
EDIT
Seems you misunderstood my answer
first put the code in your link like this:
'pwd.php'
<button onclick="window.parent.closeWin()">Close the new window (w3schools.com)</button>
second, don't include common.js in your pwd.php, since we will use the parent window function.
SECOND EDIT
You can add it in a function, but you should create different one, like this
<button onclick="child_closeWin()">Close the new window (w3schools.com)</button>
and the function should be:
function child_closeWin() {
window.parent.closeWin();
}
note that this function should be added specifically inside pwd.php only and not in index.php, just add script tag inside 'pwd.php' and add the funtion.