2

I need to close a browser window in 10 seconds on rails. I tried this code in a rails view file called callback.html.erb. Here's my code.

<center>
<p>
    Your profile has been processed. This browser window will close in 10 seconds
</p>
</center>
<script type="text/javascript">
function closeWindow() 
{
    setTimeout
    (
        function() 
        {
            window.close();
        },10000
    );
}
window.onload = closeWindow();
</script>

It doesn't seem to be working(Nothing really happens). Any idea on where I'm wrong?

Thanks in advance!

Alok Mysore
  • 606
  • 3
  • 16

3 Answers3

2

See this answer. Basically you can't close the window if you haven't opened it. Close windows that were not opened by script using javascript

Community
  • 1
  • 1
vooD
  • 2,881
  • 2
  • 25
  • 34
0

It should be:

window.onload = closeWindow;

By putting () at the end you invoke closeWindow immediately, but you want to provide a reference to this function.

dfsq
  • 191,768
  • 25
  • 236
  • 258
0

Try this with count countdown-

<p style="text-align:center">Please select a seller to process the report.<br/>
This window will close automatically within <span id="counter">10</span> second(s).</p>

        <script type="text/javascript">

            function countdown() {
            var i = document.getElementById('counter');
            i.innerHTML = parseInt(i.innerHTML)-1;
            if (parseInt(i.innerHTML)<=0) {
                 window.close();
            }
        }
        setInterval(function(){ countdown(); },1000);
        </script>
Sajjad Hossain
  • 185
  • 3
  • 14