Catalina Neshina Catalina Neshina - 5 months ago 15
Javascript Question

how to close window after php has been finished?

I have this code:

<?php
/*
creating a database
*/
echo "<script>window.close();</script>";
?>


In the end I'm trying to close the current window, but I have this error: Scripts may close only the windows that were opened by it. The window is opened physically. Could you help me with solution?

Answer

Imagine two pages, a.php and b.php.

b.php is the same in both examples:

<script>window.close();</script>

This example HTML will NOT work (a.php):

<a href="b.php">open b.php</a>

Now, this will work (a.php again):

<a href="#" onclick="window.open('b.php');return false">open b.php</a>

The reason why. Because Javascript (should) only be able to close a window that it has opened.

The first example HTML is a standard a href link. No Javascript used.

In the second example, a browser window is opened with Javascript, which can then be closed by Javascript.

PHP is a sever side script and has nothing to do with it at all. Would be best if the PHP tag was removed from the question.

Comments