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
    974
    Thanks
    19
    Thanked 212 Times in 210 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,398
    Thanks
    12
    Thanked 570 Times in 563 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
    18,144
    Thanks
    203
    Thanked 2,547 Times in 2,525 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
    •