Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-23-2009, 05:49 AM   PM User | #1
vikram8jp
New Coder

 
Join Date: Jan 2009
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
vikram8jp is an unknown quantity at this point
Question Closing a window using javascript in Firefox

Hello all,

I am trying to close a window when I press a link using javascript.
The code is simple...
Code:
<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...
Code:
<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 is offline   Reply With Quote
Old 01-23-2009, 05:53 AM   PM User | #2
vikram8jp
New Coder

 
Join Date: Jan 2009
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
vikram8jp is an unknown quantity at this point
and before I forget to mention, I have also tried out

Code:
<a href="javascript:window.close();" onclick="window.close()">CLOSE</a>
Nothing happens when I do this. The page doesn't even refresh.
vikram8jp is offline   Reply With Quote
Old 01-23-2009, 05:56 AM   PM User | #3
vikram8jp
New Coder

 
Join Date: Jan 2009
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
vikram8jp is an unknown quantity at this point
Code:
<a href="javascript:self.close();">CLOSE</a>
This doesn't work either. Nothing happens when I click the link.
vikram8jp is offline   Reply With Quote
Old 01-23-2009, 06:00 AM   PM User | #4
vikram8jp
New Coder

 
Join Date: Jan 2009
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
vikram8jp is an unknown quantity at this point
Could it be because I opening the window in a new tab using Ctrl+Click?
vikram8jp is offline   Reply With Quote
Old 01-23-2009, 06:02 AM   PM User | #5
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
You cannot close a window that was not opened by a script.
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Old 01-23-2009, 06:10 AM   PM User | #6
vikram8jp
New Coder

 
Join Date: Jan 2009
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
vikram8jp is an unknown quantity at this point
Rangana. Thanks for giving time.

Quote:
You cannot close a window that was not opened by a script.
I know that already. I said that in my first post.

Quote:
Originally Posted by vikram8jp View Post
Code:
<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.
Quote:
<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?
vikram8jp is offline   Reply With Quote
Old 01-23-2009, 06:20 AM   PM User | #7
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
My apologies, but try:
Code:
<a href="" onclick="window.close();return false;">Close this window</a>
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Old 01-23-2009, 07:18 AM   PM User | #8
vikram8jp
New Coder

 
Join Date: Jan 2009
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
vikram8jp is an unknown quantity at this point
Quote:
Originally Posted by rangana View Post
My apologies, but try:
Code:
<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.
vikram8jp is offline   Reply With Quote
Old 01-23-2009, 07:26 AM   PM User | #9
vikram8jp
New Coder

 
Join Date: Jan 2009
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
vikram8jp is an unknown quantity at this point
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.
vikram8jp is offline   Reply With Quote
Old 01-23-2009, 07:27 AM   PM User | #10
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
It's because on FF, you can't close the window that was not opened by a script.
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Old 01-23-2009, 07:48 AM   PM User | #11
vikram8jp
New Coder

 
Join Date: Jan 2009
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
vikram8jp is an unknown quantity at this point
Hmmmm.... I guess I will have to stick with the current situation only. There doesn't seem to be a workaround. Thanks anyway.
vikram8jp is offline   Reply With Quote
Old 09-29-2011, 09:49 AM   PM User | #12
fail
Regular Coder

 
Join Date: Dec 2009
Location: Hong Kong
Posts: 118
Thanks: 8
Thanked 0 Times in 0 Posts
fail is an unknown quantity at this point
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-e...e-firefox.html

Here is how I open 10 tabs in one go:

PHP Code:

// 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.

PHP Code:
// 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.
fail is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:20 AM.


Advertisement
Log in to turn off these ads.