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
    May 2011
    Posts
    122
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Simple Redirect, isn't redirecting.

    Here's my firstly:

    PHP Code:
    <?php      echo "<script language=\"javascript\" type=\"text/javascript\">  
    var g_iCount = new Number();

    // CHANGE THE COUNTDOWN NUMBER HERE - ADD ONE TO IT //
    var g_iCount = 6;

    function startCountdown(){
           if((g_iCount - 1) >= 0){
                   g_iCount = g_iCount - 1;
                   numberCountdown.innerText = 'You must wait ' + g_iCount +' seconds before your download starts';
                   setTimeout('startCountdown()',1000);
           }
    }
    function top() {
    setTimeout('startCountdown()',0000);
    setTimeout('delayer()',6000);
    }

    function delayer() {
    window.location = \"http://www.mediaphiles.net/uploads/"
    .$actualname."\"
    }
    </script>"
    ;
    ?>
    Don't worry about the $actualname variable, that is working. What I have it doing is when you click a button it starts top() which starts the countdown. Then, after 5 seconds (6 seconds) it redirects. But it doesn't seem to be working.

    -- I'm new to Javascript so I have no idea what the problem is.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I think it was a problem with the string redirect string at the end. Also innerText is not so widely used. I don't know about a php echo, but this works as normal js:

    Code:
    <body>
    <div id="numberCountdown"></div>
    <script type="text/javascript">
    
    var g_iCount = new Number();
    
    // CHANGE THE COUNTDOWN NUMBER HERE - ADD ONE TO IT //
    var g_iCount = 6;
    
    function startCountdown(){
           if((g_iCount - 1) >= 0){
                   g_iCount = g_iCount - 1;
                   document.getElementById("numberCountdown").innerHTML = 'You must wait ' + g_iCount +' seconds before your download starts';
                   setTimeout('startCountdown()',1000);
           }
    }
    function top() {
    setTimeout('startCountdown()',0000);
    setTimeout('delayer()',6000);
    }
    
    function delayer() {
    window.location = 'http://www.mediaphiles.net/uploads/'+.$actualname;
    }
    </script>; 
    
    </body>

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    although setting two timeouts counting down to the same event seems a little excessive to me, and calling a function from within itself doesn't make much sense when you can use SetInterval...

    Code:
    <body>
    <div id="numberCountdown"></div>
    <script type="text/javascript">
    
    // CHANGE THE COUNTDOWN NUMBER HERE - ADD ONE TO IT //
    var g_iCount = 6;
    
    function startCountdown(){
           if((g_iCount - 1) >= 0){
                   g_iCount = g_iCount - 1;
                   document.getElementById("numberCountdown").innerHTML = 'You must wait ' + g_iCount +' seconds before your download starts';
                   } else {
    		window.location = 'http://www.google.com'
    	}
    }
    
    function top() {
    startCountdown();
    setInterval(startCountdown,1000);
    }
    
    
    top();
    </script>; 
    
    </body>
    Last edited by xelawho; 12-25-2011 at 12:21 AM. Reason: playing around

  • Users who have thanked xelawho for this post:

    Xaqa (12-25-2011)

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    122
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Last one works, but it continuously redirects and the link it redirects to is a download link so it downloads 1,2,3,4,5 (and so on) files.
    Last edited by Xaqa; 12-25-2011 at 11:01 PM.

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Xaqa View Post
    Last one works, but it continuously redirects and the link it redirects to is a download link so it downloads 1,2,3,4,5 (and so on) files.
    The interval needs to be cancelled.

    Try this. The 'seconds' display replaces the @ in the message.

    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    </head>
    <body>
    
    <span id='tell'></span><p>
    
    <script type='text/javascript'>
    
    function timedRedirect( secs, elemId, msg, newURL )
    {
      var period,
          remain = secs,
          msgParts = msg.split( '@' ),
          elem = document.getElementById( elemId );
          
     function f()
     {        
       elem.innerHTML = msgParts[ 0 ] + remain + msgParts[ 1 ];
       
       if( remain-- == 0 )
       {
         clearInterval( period );
         location.href = newURL;   
       } 
     }
     
     function installHandler( obj, evt, func )
     {
       window.attachEvent ? obj.attachEvent(evt,func) : obj.addEventListener( evt.replace( /^on/i, "" ), func, false);
     }
     
     installHandler( window, 'onload', function(){ period = setInterval( f, 1000 ) } );    
    }
    
    timedRedirect( 5, 'tell', 'Seconds remaining before redirection: @', 'http://disney.com' );
    
    </script>
    </body>
    </html>

  • #6
    Regular Coder
    Join Date
    May 2011
    Posts
    122
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    The interval needs to be cancelled.

    Try this. The 'seconds' display replaces the @ in the message.

    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    </head>
    <body>
    
    <span id='tell'></span><p>
    
    <script type='text/javascript'>
    
    function timedRedirect( secs, elemId, msg, newURL )
    {
      var period,
          remain = secs,
          msgParts = msg.split( '@' ),
          elem = document.getElementById( elemId );
          
     function f()
     {        
       elem.innerHTML = msgParts[ 0 ] + remain + msgParts[ 1 ];
       
       if( remain-- == 0 )
       {
         clearInterval( period );
         location.href = newURL;   
       } 
     }
     
     function installHandler( obj, evt, func )
     {
       window.attachEvent ? obj.attachEvent(evt,func) : obj.addEventListener( evt.replace( /^on/i, "" ), func, false);
     }
     
     installHandler( window, 'onload', function(){ period = setInterval( f, 1000 ) } );    
    }
    
    timedRedirect( 5, 'tell', 'Seconds remaining before redirection: @', 'http://disney.com' );
    
    </script>
    </body>
    </html>
    Thanks but if I wanted that to start when someone clicks a button... what function do I tell it to start?


  •  

    Posting Permissions

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