Javascript: how to open a page then wait for few seconds open another page in the same window

I tried to open an url in a window then wait for few seconds and opened another url in the same window. But the script doesn't work. When run it gives a blank window. I am new in Javascript. Can someone please help me?

I want to run it in Google Chrome as well.

The script is as follows:

my_window=window.open("","mywindow");
my_window.location="http://www.yahoo.com";
sleep(10000);
my_window.location="http://www.youtube.com";
sleep(10000);
my_window.close();

function sleep(delay)
{
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}

Answers:

Answer

You could give this a try:

<script type="text/javascript">
function def()
{
my_window.location="http://www.yahoo.com";
setTimeout("abc()", 3000);
}

function abc()
{
alert("Delayed 3 seconds");
my_window.location="http://www.youtube.com";
}
</script>
Answer

In general, it is inadvisable to use blocking loops in javascript. In your case, you would want to use something like setTimeout or setInterval. This code should work:

var win = window.open("http://foo.com");
setTimeout(function(){
    win.location = "http://bar.com";
    setTimeout(function(){
        win.close();
    }, 10000);
}, 10000);
Answer

Tested and this one works but the popup blocker appears

<!DOCTYPE html>
<html>
    <head>
        <script>
        function open_win() 
        {
            setTimeout("go('http://www.yahoo.com')", 5000);

            setTimeout("go('http://www.youtube.com')", 10000);

        }

        function go(url){
            window.open(url);
        }
    </script>
</head>

<body>
<form>
    <input type="button" value="Open Win" onclick="open_win()">
</form>
</body>
</html>
Answer

UPDATED

I wrote the following HTML and it worked fine for me showing what is required in your post:

<html>
    <head>
        <script language="JavaScript" type="text/javascript">

        var my_window;

        function OpenWin()
        {
            my_window=window.open("http://www.yahoo.com", "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=1000, height=800, top=10, left=10");
            setTimeout("GoUrl('http://www.youtube.com')", 10000);
        }
        function GoUrl(Url)
        {
            my_window.location=Url;
        }
        </script>
    </head>
    <body>
        <button onclick="OpenWin()">Open Window</button>
    </body>
</html>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.