Redirect Script

Hi all,

I had a redirect script in my web page which has stopped working. I wonder if somebody could tell me what is wrong with this code? I tried adding an alert to the function to see if it was running and the alert doesn’t display so I assume that means it isn’t running.

Here is the javascript code:

<script>
redirTime = "5000";
redirURL = "index.php?/category/5/start-1?slide=true";

 function redirTimer()

   {

    settime=self.setTimeout("self.location.href = redirURL;",redirTime);
    document.getElementById('slideshow').innerHTML='<a href=# onclick="stopredir()">Stop Slideshow</a>';
    document.getElementById('next-nav').innerHTML='<a class="next-link" href="index.php?/category/5/start-1?slide=true" rel="next"><img class="next-icon" src="/gallery/themes/default/images/next.png" /></a>';
	document.getElementById('prev-nav').innerHTML='<a class="prev-link" href="/?slide=true" rel="prev"><img class="prev-icon" src="/gallery/themes/default/images/prev.png" /></a>';
	
	alert(Redirect Script is running);
	

   {


      function stopredir()
	
       {

        window.clearTimeout(settime);
        document.getElementById('slideshow').innerHTML='<a href=# onclick="redirTimer()">Start Slideshow</a>';
        document.getElementById('next-nav').innerHTML='<a class="next-link" href="index.php?/category/5/start-1?slide=false" rel="next"><img class="next-icon" src="/gallery/themes/default/images/next.png" /></a>';
        document.getElementById('prev-nav').innerHTML='<a class="prev-link" href="/?slide=false" rel="prev"><img class="prev-icon" src="/gallery/themes/default/images/prev.png" /></a>';

   {

</script>

And this is the Body tag to show that I am calling it on page load:

<body id="theCategoryPage" onload="redirTimer()">

I hope somebody can help me. I’ve been trying to get this working all day.

The alert.contains invalid content. You would want to alert a string instead:

alert(Redirect Script is running);

alert('Redirect Script is running');

How you currently define your functions is incorrect.


function redirTimer() 
{ 
    ...
{

Function definitions start with an opening curly brace, and close with a closing curly brace. Also, JavaScript can have some syntax errors when curly braces start on a new line, so in JavaScript, always place the curly brace at the end of the line:


function redirTimer() { 
    ...
}

Things should work a bit better after you take care of those types of issues.

Hi Paul,

I tried your suggestions and nothing has changed. The script does not run. Again, the debug alert is not displaying either which confirms the script is not being called.

Here is the syntax now:

<script>
redirTime = "5000";
redirURL = "index.php?/category/5/start-1?slide=true";

 function redirTimer()  {
    settime=self.setTimeout("self.location.href = redirURL;",redirTime);
    document.getElementById('slideshow').innerHTML='<a href=# onclick="stopredir()">Stop Slideshow</a>';
    document.getElementById('next-nav').innerHTML='<a class="next-link" href="index.php?/category/5/start-1?slide=true" rel="next"><img class="next-icon" src="/gallery/themes/default/images/next.png" /></a>';
	document.getElementById('prev-nav').innerHTML='<a class="prev-link" href="/?slide=true" rel="prev"><img class="prev-icon" src="/gallery/themes/default/images/prev.png" /></a>';
	
	alert('Redirect Script is running');
	
   {

      function stopredir()  {
        window.clearTimeout(settime);
        document.getElementById('slideshow').innerHTML='<a href=# onclick="redirTimer()">Start Slideshow</a>';
        document.getElementById('next-nav').innerHTML='<a class="next-link" href="index.php?/category/5/start-1?slide=false" rel="next"><img class="next-icon" src="/gallery/themes/default/images/next.png" /></a>';
        document.getElementById('prev-nav').innerHTML='<a class="prev-link" href="/?slide=false" rel="prev"><img class="prev-icon" src="/gallery/themes/default/images/prev.png" /></a>';
   {
</script>

I see that you have fixed the alert part, well done on that, but your function definitions are still screwed up.