...

View Full Version : Closing a window using javascript in Firefox



vikram8jp
01-23-2009, 06:49 AM
Hello all,

I am trying to close a window when I press a link using javascript.
The code is simple...

<a href="" onclick="window.close()">Close this window</a>

This works as well, and I know that it will work only on windows opened by javascript. That's ok as well.

My question is that in Firefox, I can open a link a new tab. So I open a new tab by clicking on a link like this...

<a href="somepage.html" target="_blank">OPEN</a>

Now when I try to close this tab using javascript, after closing it refreshes the page. This I know is happening because, I did href="".

What's the workaround for this problem....

vikram8jp
01-23-2009, 06:53 AM
and before I forget to mention, I have also tried out


<a href="javascript:window.close();" onclick="window.close()">CLOSE</a>

Nothing happens when I do this. The page doesn't even refresh.

vikram8jp
01-23-2009, 06:56 AM
<a href="javascript:self.close();">CLOSE</a>

This doesn't work either. Nothing happens when I click the link.

vikram8jp
01-23-2009, 07:00 AM
Could it be because I opening the window in a new tab using Ctrl+Click?

rangana
01-23-2009, 07:02 AM
You cannot close a window that was not opened by a script (http://developer.mozilla.org/en/docs/DOM:window.close).

vikram8jp
01-23-2009, 07:10 AM
Rangana. Thanks for giving time.


You cannot close a window that was not opened by a script.

I know that already. I said that in my first post.




<a href="" onclick="window.close()">Close this window</a>

This works as well, and I know that it will work only on windows opened by javascript. That's ok as well.


I also said that this will open a link and the window opened by clicking on this link is closed as well.

<a href="somepage.html" target="_blank">OPEN</a>

But what about Ctrl+Clicking. And if that doesn't work (which its not), then is there some workaround suggested?

rangana
01-23-2009, 07:20 AM
My apologies, but try:


<a href="" onclick="window.close();return false;">Close this window</a>

vikram8jp
01-23-2009, 08:18 AM
My apologies, but try:


<a href="" onclick="window.close();return false;">Close this window</a>


Nope. That doesn't work either. Neither the page refreshes, nor the window closes. :confused:

vikram8jp
01-23-2009, 08:26 AM
I have been trying around, but this thing doesn't work only in Firefox tabs. In IE tabs, Google Chrome tabs, and Safari tabs it works fine.

rangana
01-23-2009, 08:27 AM
It's because on FF, you can't close the window that was not opened by a script.

vikram8jp
01-23-2009, 08:48 AM
Hmmmm.... I guess I will have to stick with the current situation only. There doesn't seem to be a workaround. Thanks anyway.

fail
09-29-2011, 10:49 AM
This is sooooo old, but I was having the same issue - I want to close a tab automatically if a certain event does not happen. If one poor soul appreciates it I will be happy.

This link gave me the core idea:

http://www.yournewdesigner.com/css-experiments/javascript-window-close-firefox.html

Here is how I open 10 tabs in one go:




// Source Page

echo "<tr><td><a href='http://www.".$new[9]."'
onclick=\"window.open('http://www.".$new[0]."/');
window.open('http://www.".$new[1]."/');
window.open('http://www.".$new[2]."/');
window.open('http://www.".$new[3]."/');
window.open('http://www.".$new[4]."/');
window.open('http://www.".$new[5]."/');
window.open('http://www.".$new[6]."/');
window.open('http://www.".$new[7]."/');
window.open('http://www.".$new[8]."/')\" target='new300/'>All 10</a>

// $new[] = strip.php?url=http://www.some_URL.com



It will check the HTML and looks for something I want to know.



// Target Page
<script language="javascript" type="text/javascript">
function closeWindow() {
window.open('','_parent','');
window.close();
}
</script>

if ($event != '') {

echo "<script> closeWindow() </script>";

}



If the PHP code does not find anything that I want then the tab will close automatically.

It works on Firefox (7.0 now) and on Chrome too. Did not check other browser.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum