• Login
  • Become a blogger
  • PHP function for Javascript redirect

    Posted by thinkdj on January 26th, 2008
    9 Comments | 7,077 pageviews

    The following code is basically JS, but has been made into a PHP function so that it could be invoked with different parameters as per our needs.

    <?php
    function js_redirect($url, $seconds=5) {
    echo
    "<script language=\"JavaScript\">\n";
    echo
    "<!-- hide code from displaying on browsers with JS turned off\n\n";
    echo
    "function redirect() {\n";
    echo
    "window.location = \"" . $url . "\";\n";
    echo
    "}\n\n";
    echo
    "timer = setTimeout('redirect()', '" . ($seconds*1000) . "');\n\n";
    echo
    "-->\n";
    echo
    "</script>\n";

    return true;
    }

    js_redirect(http://blogulate.com,1); //Redirects to blogulate.com in 1 second.

    ?>

    Eg Usage :
    js_redirect(http://google.com,5); //Redirects to Google after 5 seconds.

    9 Responses »

    1. yeahboy says:

      oh wtf.. nevermind. the code is being messed up by the blog.

    2. yeahboy says:

      i guess you can make it as simple as this:

      function js_redirect($url, $seconds=1)
      {
      echo ”;
      echo ‘function redirect() {’;
      echo ‘window.location = “‘ . $url . ‘”;’;
      echo ‘}’;
      echo ‘timer = setTimeout(”redirect()”, “‘ . ($seconds*1000) . ‘”);’;
      echo ”;
      return true;
      }

    3. yeahboy says:

      this ain’t working…

    4. Kirkorov says:

      Haha. It’s funny.

    5. thinkdj says:

      You could also view the source of the following page for another JS redirect:

      http://www.tizag.com/javascriptT/examples/timedelay.php

    6. thinkdj says:

      Toraj,
      I think the best for you would be a simpler META tag

      <meta http-equiv="refresh" content="0;URL=http://google.com">

      Just add this in the within the <head> </head> of your html. Change 0 to an integer value for time to wait before redirecting..

    7. toraj says:

      hello thinkdj
      so can i direct with this code myweblog to website ?

    8. thinkdj says:

      Hi toraj,
      Sorry about that. It’s a JavaScript echoed using PHP so that the destination URL and time before redirection could be called dynamically..
      Eg :

      js_redirect(http://google.com,5); //Redirects to Google after 5 seconds.

      js_redirect(http://google.co.in,15); //Redirects to Google.co.in after 15 seconds.

    9. toraj says:

      what is this code? please say more about that
      thanks a lot

    Trackbacks/Pingbacks

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    More from "PHP"

    Find what you are looking for

    Recently added

    Categories

    Blogulate

    Keeping you updated of the latest fads and developments of the industry via amalgamated web content.

    Join us

    Post articles on blogulate and monetize them. Learn more »

    Stay updated

    Subscribe now and stay aware of the latest trends and happenings on the www. Get notified via email when we update.

    Enter your email address:

    Blogulate is based on Magnus premium theme by WizeDesigns
    © 2009 Blogulate. Some rights reserved.