Forum Moderators: open

Message Too Old, No Replies

<Java Script Question?>

Close window, open new window if not open already?

         

Bluetuna

10:51 pm on Jan 9, 2003 (gmt 0)

10+ Year Member



Can you have a script that closes a page and opens the index page if not already open?

tedster

11:34 pm on Jan 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can only close a window with javascript if javascript opened it, not if the user opened it. The syntax is window.close()

However, I'm not clear on how to check if a particular document (such as index.html) is displayed anywhere. Javascript can't "look" at other windows without permission, either implicit -- because you opened the window with javascript and hence know how that object is named, or explicit -- as in a signed script.

DrDoc

8:11 pm on Jan 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the 2nd window was opened by a JavaScript, then you can do this check in that document by using the window.opener syntax.

Example:

<script type="text/javascript">
function myFunc() {
if(!window.opener.location.href="http://www.example.com/index.html") {
window.opener.location.href="http://www.example.com/index.html";
}
self.close();
}
</script>
<a href="javascript:myFunc()">Close window</a>