Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Open window onClick with setTimeout

    I'm trying to pass the URL of a link to a function, then delay opening the window. I get an undefined error. Any help would really be appreciated.

    Here is my code:

    PHP Code:
    <head>
    <
    script>
        function 
    openWin(test) {
            
    setTimeout("window.open(test)"5000);
        }
    </script>
    </head>

    <body>
    <a href="http://www.example.com" onClick="openWin(this); return false;">Test</a>
    </body> 

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Quote Originally Posted by dlg0351 View Post
    I'm trying to pass the URL of a link to a function, then delay opening the window. I get an undefined error. Any help would really be appreciated.

    Here is my code:

    PHP Code:
    <head>
    <
    script>
        function 
    openWin(test) {
            
    setTimeout("window.open(test)"5000);
        }
    </script>
    </head>

    <body>
    <a href="http://www.example.com" onClick="openWin(this); return false;">Test</a>
    </body> 
    This works for me in ie7.....

    Code:
    <head> 
    <script> 
        function openWin(test) { 
            setTimeout(function(){window.open(test)}, 5000); 
        } 
    </script> 
    </head> 
    
    <body> 
    <a href="http://www.example.com" onClick="openWin(href); return false;">Test</a> 
    </body>

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    First: You need to make sure that you are accessing the link (so the "href" attribute) instead of the <a> element

    Second: You need to make sure to use a function reference as the first parameter of setTimeout. This will create a closure which will automatically keep available the parameter "test"

    Code:
    <head>
    <script>
        function openWin(test) {
            setTimeout(function() {window.open(test);}, 5000);
        }
    </script>
    </head>
    
    <body>
    <a href="http://www.example.com" onClick="openWin(this.href); return false;">Test</a>
    </body>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by DaveyErwin View Post
    This works for me in ie7.....
    And in IE8

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Awesome! Thanks so much!

  • #6
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts
    IE? Who uses that? haha, kidding!


  •  

    Posting Permissions

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