...

View Full Version : Open window onClick with setTimeout



dlg0351
10-06-2011, 08:11 PM
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:



<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>

DaveyErwin
10-06-2011, 08:50 PM
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:



<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.....


<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>

devnull69
10-06-2011, 08:52 PM
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"



<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>

Philip M
10-06-2011, 08:53 PM
This works for me in ie7.....


And in IE8 :)

dlg0351
10-06-2011, 09:05 PM
Awesome! Thanks so much!

dlg0351
10-06-2011, 09:06 PM
IE? Who uses that? haha, kidding!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum