SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Addict
    Join Date
    Dec 2002
    Location
    Philadelphia
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Firefox won't let me close a window with Javascript

    Perhaps this browser is forcing me to write proper code?

    I usually do

    a href=javascript:window.close()

    But Firefox does nothing. I looked in the Firefox Javascript Console and found the reason:

    "Scripts may not close windows that were not opened by script."

    How can I get around this? (In IE I usually get a confirmation alert, but it allows me to proceed). I have a 5 page series of forms where the user goes through the forms but hitting "submit" at the end of every page, and all the data is stored using session variables.

    thanks

  2. #2
    I am obstructing justice. bronze trophy fatnewt's Avatar
    Join Date
    Jul 2002
    Location
    Ottawa, Canada
    Posts
    1,766
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never put JavaScript in the href attribute of a link. It makes for bad usability/accessibility.

    If the window isn't opened by JavaScript, it seems that Firefox won't let JavaScript close it. Good for them!

    Chances are that most Firefox users are capable of using the little "X" to close their own windows.
    Colin Temple [twitter: @cailean]
    Web Analyst at Napkyn


  3. #3
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,449
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by fatnewt
    If the window isn't opened by JavaScript, it seems that Firefox won't let JavaScript close it. Good for them!
    IE does this, too. Encountered it just last week.

  4. #4
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Southwest Florida
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't know FF had that feature, but I consider it exactly that: a feature. You shouldn't be allowed to close windows that you didn't open.

    Have you tried having your javascript open the first page of your form in a new window? Then it seems like you should be able to use javascript to close it when the user is done.

    Or you could just display a "thank you you're done page" in which you clear all the session variables.

    Frankly, I have to say, I hope you don't find a way to close windows that you didn't open.

  5. #5
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Southwest Florida
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fatnewt
    Never put JavaScript in the href attribute of a link. It makes for bad usability/accessibility.
    Fatnewt, I've always used a standard href link, plus the javascript, when I need to use javascript in a link tag. As in, <a href="page.htm" onclick="myjavascript; return false;">. Then when the user has javascript turned off, or it's unavailable for any reason, the link itself still works. It also lets people open the link in a new window or a new tab, or copy the link, or whatever.

    I've also always believed that this method addressed the issue of usability and accessibility. Am I wrong? Or are you just referring to using javascript and nothing but javascript as being an accessibility/usability problem in links?

  6. #6
    I am obstructing justice. bronze trophy fatnewt's Avatar
    Join Date
    Jul 2002
    Location
    Ottawa, Canada
    Posts
    1,766
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And that's the way you should do it sonjay. If you want to provide additional functionality with client-side languages like JavaScript, ensure that the page degrades gracefully in user agents that don't support the language.

    I was specifically referring to JavaScript being in the href property of a link. If you want to add more to onclick (etc) attributes, or externally, that's fine as long as the link is still usable when JavaScript is disabled.
    Colin Temple [twitter: @cailean]
    Web Analyst at Napkyn


  7. #7
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Southwest Florida
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fatnewt
    I was specifically referring to JavaScript being in the href property of a link.
    Yep, I see now that you did specifically say JavaScript in the href attribute. I missed that and thought you were saying to not ever use JavaScript in a link. And I thought my way was accessible. So I thought that one of us must be mistaken.

    BTW, I enjoyed the understatement here:
    Chances are that most Firefox users are capable of using the little "X" to close their own windows.

  8. #8
    SitePoint Addict
    Join Date
    Dec 2002
    Location
    Philadelphia
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, here's the deal. I am working with a designer on a project. The design has custom buttons for "continue", "back" , and "cancel" in the process of someone signing up for an online account.

    All of this is happening in a new window that has been openened on top of an online store.

    If someone has entered sensitive information, and they want to cancel the procedure, many people (myself included, before I began coding years ago), would NOT want to close the new window themselves, without knowing whether or not they could completely stop the transmission of information.

    Do you suggest that I just take them to a screen that says "your account has been cancelled" and instruct them to close the window? I guess that's a compromise.

  9. #9
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Southwest Florida
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shem
    Do you suggest that I just take them to a screen that says "your account has been cancelled" and instruct them to close the window? I guess that's a compromise.
    I would consider that not a compromise at all, but a good user interface. If someone's cancelling such a process, yes, I think they would want confirmation that the process was cancelled, as opposed to the window just up and disappearing.

    Anyone who doesn't know how to click the standard "close" button on their browser window almost certainly isn't going to be opening an account with you and entering sensitive information. I know that users can be unbelievably thick-headed, but I'd bet you can safely trust your users to close the window when they get a page that says 'Your account has been cancelled. Please close this window to return to where you were."

  10. #10
    SitePoint Addict
    Join Date
    Jul 2000
    Posts
    201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the code I use to close a popup. It works in Firefox, Opera and IE (without showing the confirmation box). You just need to put the function that closes the popup in the page that opens the popup.
    HTML Code:
    <html>
    <head>
    <script language="Javascript">
    <!-- Begin
    function surveypopup(URL) {
      var surveywindow = window.open(URL, 'surveypopupname',
      'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=350');
    }
    function surveypopupClose() {
       surveywindow.close()
    }
    // End -->
    </script>
    <title>Your title</title>
    </head>
    <body onLoad="javascript:surveypopup('/popup.html')">
    </body>
    </html>
    Here is the popup page:
    HTML Code:
    <html>
    <head>
    <title>Popup title</title>
    </head>
    <body>
    <center><p><a href="javascript:opener.surveypopupClose();">Close This Window</a></p></center>
    </body>
    </html>

  11. #11
    SitePoint Addict
    Join Date
    Dec 2002
    Location
    Philadelphia
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is good...I will try. But how do you open up a url that is the submit target for a FORM ? Also, I am sending variables to the new page that will be in the popup window also.

    I tried this but it did not work:
    <form action="javascript:surveypopup('cart/cart.php?action=showcart')" method="post">

  12. #12
    SitePoint Addict
    Join Date
    Dec 2002
    Location
    Philadelphia
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, of course....I had to do

    onSubmit= instead of action=

    Ok, that's fixed....the window opens ok, but the function you provided will not close it. Can you help with that? Thanks.

  13. #13
    SitePoint Addict
    Join Date
    Dec 2002
    Location
    Philadelphia
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well...i just decided to close it with

    javascript:window.close()

    and that works fine in Firefox with no warning. So I guess it's ok. But your method, FauxPas, intrigues me. Unless the method itself is a FauxPas.


Bookmarks

Posting Permissions

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