Page 1 of 1

Redirect

Posted: Mon Nov 07, 2005 2:09 pm
by Luke
How come this doesn't work? I never really use javascript... but as far as I know this should be ok... ??

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
 <head>
  <title>Paradise Direct - Northern California's Premiere Web Design & Hosting Service</title>
  <script language="javascript" type="text/javascript">

function reDirect(sTargetURL="http://www.paradisedirect.com", offSet=3){
	setTimeout(location.replace(sTargetURL), offSet)
}

  </script>
 </head>
 <body onLoad="reDirect()">
  You are being redirected...
 </body>
</html>

Posted: Mon Nov 07, 2005 2:23 pm
by foobar
Why don't you try to read the debug messages in a _proper_ browser (not IE).

The error is quite simple. There are no default values for function parameters in JS.

Instead of this:

Code: Select all

function reDirect(sTargetURL="http://www.paradisedirect.com", offSet=3){
   setTimeout(location.replace(sTargetURL), offSet)
}
use this:

Code: Select all

function reDirect(sTargetURL, offSet=3){
   if (sTargetURL == '' || typeof sTargetURL == 'undefined') sTargetURL = "http://www.paradisedirect.com";
   if (parseInt(offSet) == 0 || typeof offSet == 'undefined') offSet = 3;
   else offSet = parseInt(offSet);
   setTimeout(location.replace(sTargetURL), offSet)
}

Posted: Mon Nov 07, 2005 2:26 pm
by Luke
Oh... I had no idea you couldn't assign defaults... no wonder my javascript functions never work. Thanks foobar.