0

With the code below, I can get the web page to automatically submit the form properly. I'm trying to figure out how to have the script delay for 5 seconds, then close the window without asking the user if they want the window to close.

This page will be used inside our company to launch a product provided by a vendor. Because it will be inside our network, the window will most likely be IE for anyone who uses it.

I cannot get the script to close the window after 5 seconds. I am trying to accomplish this without using jQuery. This is what I have so far.

<html>
<body>
<br/>
<h3>Launching GE Web Viewer ...  Please Wait</h3>
<br/>
    <form id="frmLaunchViewer" method="POST" action="https://geview.abc.org/startwebviewer.jnlp">
        <input type="hidden" name="user" value="MyUserName">
        <input type="hidden" name="password" value="MyPassword">

        <script>
            document.getElementById('frmLaunchViewer').submit();
            window.setTimeout(CloseMe, 500);
        </script>   

        <script>
            function CloseMe() 
            {
              window.close();
            }
        </script>
    </form> 
</body>

klenium
  • 2,468
  • 2
  • 24
  • 47
crjunk
  • 949
  • 4
  • 21
  • 40
  • Close an opened page by javascript =/= close the browser, a program. – klenium Aug 25 '15 at 21:18
  • 1
    You generally can't close a browser window unless it's a window your code opened with `window.open()`. – Pointy Aug 25 '15 at 21:18
  • 1
    5 seconds is `5000` milliseconds (you forgot a `0`). And you might want to read this: http://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome – blex Aug 25 '15 at 21:19
  • 2
    If you're posting the form, the page will refresh, wiping any existing timers. Set some sort of variable, perhaps `localStorage`, to indicate that you've submitted the form then you can _try_ redirect - closing probably won't work. – Evan Knowles Aug 25 '15 at 21:20
  • Appears to be a simple typo; 500 is not 5 seconds, it's half a second. – Popnoodles Aug 25 '15 at 21:21
  • window.close() may close only the windows that were opened by it. – Alejandro Garcia Anglada Aug 25 '15 at 21:26
  • That is what I was afraid of Pointy. I'm assuming opening the browser to the page using a .bat file would make no difference, correct? Thanks for catching my typo blex. I'm not 100% sure that the 5 second delay is needed in order for the form submission to go through. This was more for the benefit of the user who clicks on the shortcut. – crjunk Aug 26 '15 at 11:58

2 Answers2

0

As Evan Knowles said, if you submit a form, that'll refresh the page. You should use ajax to post the form:

var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
       if(xmlhttp.status == 200){
           window.close();
       }
    }
}

xmlhttp.open("POST", "https://geview.abc.org/startwebviewer.jnlp", true);
xmlhttp.send(new FormData(document.getElementById("frmLaunchViewer")));

Waiting 5 seconds is useless, unless sendig the form's data takes 5 seconds. It's better to use an ajax callback than hoping that 5 seconds is enough to finish the process. If you really need that, see blex's comment.

klenium
  • 2,468
  • 2
  • 24
  • 47
0

Try this -

    <script>
        document.getElementById('frmLaunchViewer').submit();
        window.setTimeout(CloseMe, 5000);
    </script>   

    <script>
        function CloseMe() 
        {
          close();
        }
    </script>

Please check these -

How to close current tab in a browser window?

http://www.w3schools.com/jsref/met_win_settimeout.asp

Community
  • 1
  • 1
SpritsDracula
  • 1,080
  • 9
  • 16