SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Redirect Script

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code JavaScript:
    <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:

    Code HTML4Strict:
    <body id="theCategoryPage" onload="redirTimer()">

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

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    The alert.contains invalid content. You would want to alert a string instead:

    Code:
    alert(Redirect Script is running);
    Code javascript:
    alert('Redirect Script is running');

    How you currently define your functions is incorrect.

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

    Code javascript:
    function redirTimer() { 
        ...
    }

    Things should work a bit better after you take care of those types of issues.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2010
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code JavaScript:
    <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>

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by CBResources View Post
    I tried your suggestions and nothing has changed.
    I see that you have fixed the alert part, well done on that, but your function definitions are still screwed up.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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