Javascript close() not working in Safari.

Discussion in 'Web Design and Development' started by yellow, Aug 24, 2009.

  1. yellow Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #1
    I have a form to submit a trouble ticket via the web, it takes a bit for the form to process after the "submit" button is hit. Consequently there's people who click,click,click,click away at it and we get n submissions of the same ticket.

    So I'm trying to use a window to appear to stop that from happening while the form processes in the background. At the end of the javascript that passes the gathered info to the form for processing, I call the function below.

    New window ("stop clicking submit, dumbs***") opens, takes focus, and should disappear of it's own accord in 8 seconds.

    Using the following code:

    Code:
    function openWin()
    {
    myWindow=window.open('http://domain.com/blah','','width=800,height=600');
    myWindow.setTimeout('close();',8000);
    myWindow.focus();
    }
    
    Well, this works like a charm in IE7, IE8, FF3.x in Mac & Win, but not in Safari. The window never closes.

    I've tried using the debugger in Safari to figure out the problem and reading the dev documentation on the apple dev site, but I'm not coming up with any answers. I'd prefer to get the correct javascript into the main page where all this is defined and called from, and not put a separate close() in the HTML of the opened window. That just seems sloppy, and lord knows, it's sloppy enough as it is.

    Thanks for any help/advice.
     
  2. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #2
    I too have tried it in Firefox, IE8, and Safari, but for me it doesn't work in any of them. The window opens (if you turn off the pop-up blocker) but it never closes.

    Wish I could help.
     
  3. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
    Code:
    window.close();
    or you may need,
    Code:
    myWindow.setTimeout(function(){window.close();}, 8000);
     
  4. yellow thread starter Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #4
    Yeah, I've tried those variations too and they won't work either.

    Works in everything except Safari.
    Code:
    function openWin()
    {
    	myWindow=window.open('http://domain.com/blah','','width=775,height=500');
    	myWindow.setTimeout('window.close();',4000);
    	myWindow.focus();
    
    }
    
    And this doesn't work in anything.
    Code:
    function openWin()
    {
    	myWindow=window.open('http://domain.com/blah,'','width=775,height=500');
    	myWindow.setTimeout(function(){window.close();},4000);
    	myWindow.focus();
    
    }
    I've also tried (found here):

    Code:
    function openWin()
    {
            myWindow=window.open('http://domain.com/blah,'','width=775,height=500');
    	setTimeout(toodle, 4000)
    	function toodle() 
            {
    		if (myWindow != '' &&
    		!myWindow.closed &&
    		myWindow.name !== undefined) 
            {
    			myWindow.close();
    	}
         }
    }
    
    EDIT:

    Even stranger, this does work (a test set from w3schools):
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function openWin()
    {
    myWindow=window.open('','','width=200,height=100');
    myWindow.document.write("This is 'myWindow'");
    myWindow.focus();
    myWindow.setTimeout('window.close();',2000);
    }
    </script>
    </head>
    <body>
    
    <input type="button" value="Open 'myWindow'" onclick="openWin()" />
    
    </body>
    </html>
    :confused:

    So I tried this as a test (works in FF/win):
    Code:
    function openWin()
    {
    	myWindow=window.open('','','width=775,height=500');
             myWindow.document.write("Stop clicking Submit please");
    	myWindow.focus();
             myWindow.setTimeout('window.close();',4000);
    }
    In Safari, opens window, no text, closes window in 4000ms. <ELBOW>
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    Actually, let's make this simpler and address the problem you're trying to resolve, multiple submits.

    form with onsubmit="OurFuntion();"
    and input type="submit" id="submit" />

    PHP:
    function OurFunction() {
      
    // Disable submit button now that it has been pushed
      
    document.getElementById('submit').disabled true;
      
    // do other stuff
    }
     
  6. yellow thread starter Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #6
    Thanks, that didn't work for me either, but I don't have access to the form declaration (don't ask).
    So, I just opted for the ugly way and put an "onload" in the pop-up window that closes after 8.5 seconds.

    Thanks for all your help though!!
     
  7. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #7
    Can't you just use an onclick() event to disable the button after it has been clicked?

    Ah :eek:.
     
  8. yellow thread starter Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #8
    The company as a whole has a web dev team, they create "templates" so the look and feel of company pages is the same across the board and provide us with "edit" right through some craptacular editor. And editor that strips out all backslashes, which makes it difficult to add a newline into javascript to output info on different lines of their form. I have to add a \ to a variable every time I edit this. This crap is so old they're still using <h1> and <strong>. :eek:
     
  9. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    Using <h1></h1> is good for SEO purposes as search engines look for it to get a context for the page in question which presumably offers more information than the page title which is often site or section wide.
     
  10. yellow thread starter Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #10
    Except that these are all intranet junk that no search engines ever hit. Their "editor" loves to add empty divs all over the place.
     

Share This Page