Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts

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

  • #2
    New Coder
    Join Date
    Jan 2009
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Code:
    <a href="javascript:self.close();">CLOSE</a>
    This doesn't work either. Nothing happens when I click the link.

  • #4
    New Coder
    Join Date
    Jan 2009
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Could it be because I opening the window in a new tab using Ctrl+Click?

  • #5
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #6
    New Coder
    Join Date
    Jan 2009
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

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

  • #7
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    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

  • #8
    New Coder
    Join Date
    Jan 2009
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #9
    New Coder
    Join Date
    Jan 2009
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    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

  • #11
    New Coder
    Join Date
    Jan 2009
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hmmmm.... I guess I will have to stick with the current situation only. There doesn't seem to be a workaround. Thanks anyway.

  • #12
    Regular Coder
    Join Date
    Dec 2009
    Location
    Hong Kong
    Posts
    119
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •