Catalina Neshina Catalina Neshina - 2 years ago 97
Javascript Question

how to close window after php has been finished?

I have this code:

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 Source

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

b.php is the same in both examples:


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="'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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download