What's wrong with this Javascript Redirect Code???

10 replies
Can't get this to work... It's driving me crazy.... Do you see anything wrong?

<SCRIPT LANGUAGE="JavaScript">
var readCookie = getCookie("upgrade");

if (readCookie("upgrade") != null){
// if cookie already exists, go to url
document.location = "lesson1";
} else {
// else go someplace else
document.location = "lesson2";
};
</script>
#code #javascript #redirect #wrong
  • Profile picture of the author wayfarer
    you're looking for window.location, not document.location. Document.location is read-only in some browsers.

    And where is the getCookie function being defined? That isn't a standard function and will have to be supplied somewhere.
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[2048530].message }}
  • Profile picture of the author Mirnova
    Here's the latest code someone else wrote to fix. Still isn't working though (no a getCookie function isn't defined. I thought that was built into javascript??? If you were to assign a var to = a cookie how would you do it?

    Anyway, latest code below (not working)
    Code:
    <script type = "text/javascript">
    var readCookie = getCookie("upgrade");   // the cookie of that name must exist and returns the cookie value.
    alert (readCookie);  // for testing
    if (readCookie) {
    //if cookie already exists, go to url
    window.location = "lesson1";  // a file or URL
    }
    else {
    // else go someplace else
    window.location = "lesson2";
    }
    </script>
    {{ DiscussionBoard.errors[2051300].message }}
    • Profile picture of the author danmart
      Originally Posted by Mirnova View Post

      Here's the latest code someone else wrote to fix. Still isn't working though (no a getCookie function isn't defined. I thought that was built into javascript??? If you were to assign a var to = a cookie how would you do it?

      Anyway, latest code below (not working)
      Code:
      <script type = "text/javascript">
      var readCookie = getCookie("upgrade");   // the cookie of that name must exist and returns the cookie value.
      alert (readCookie);  // for testing
      if (readCookie) {
      //if cookie already exists, go to url
      window.location = "lesson1";  // a file or URL
      }
      else {
      // else go someplace else
      window.location = "lesson2";
      }
      </script>
      No, GetCookie() is not a built in javascript function. But try this it should work:
      Code:
      function getCookie(c_name)
      {
      if (document.cookie.length>0)
        {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
          {
          c_start=c_start + c_name.length+1;
          c_end=document.cookie.indexOf(";",c_start);
          if (c_end==-1) c_end=document.cookie.length;
          return unescape(document.cookie.substring(c_start,c_end));
          }
        }
      return "";
      }
      Also, Lesson1 and Lesson2 have to be defined somewhere so the javascript knows where to redirect to.

      Dan
      {{ DiscussionBoard.errors[2051612].message }}
  • Profile picture of the author stma
    Some folks won't have javascript enabled. Have you considered doing this in another language?
    {{ DiscussionBoard.errors[2051810].message }}
    • Profile picture of the author Mirnova
      The reason I'm doing it in javascript (and not PHP) is because I need to place it in Wordpress posts.

      Is there another language that would work within a wordpress post other then javascript?
      {{ DiscussionBoard.errors[2052145].message }}
  • Profile picture of the author mattalways
    Within all wordpress posts? You could add something to the single.php file...
    Signature

    Quit wasting your money! If you need a website, get me to do it right! I'll probably even do it for less! Design/Development/Software, I'm your guy! matt@snidge.com
    {{ DiscussionBoard.errors[2053243].message }}
  • Profile picture of the author stma
    That's the easiest way really - or add to your header if you want it to happen to every one no matter if they go to a page or tag or category page.
    {{ DiscussionBoard.errors[2053301].message }}
  • Profile picture of the author Mirnova
    I need page by page cookie control and redirect. So editing the wordpress or template files isn't an option.

    Only other option I can imagine is with .htaccess ???
    {{ DiscussionBoard.errors[2053350].message }}
    • Profile picture of the author Frank Wright
      Code:
       
      <script type = "text/javascript">
      var readCookie = getCookie("upgrade");   // the cookie of that name must exist and returns the cookie value.
      if (readCookie) 
      {
       //if cookie already exists, go to url
       window.location = "lesson1";  // a file or URL
      }
      else 
      {
       // else go someplace else
       window.location = "lesson2";
      }
      function getCookie(name) 
      {
       var nameEQ = name + "=";
       var ca = document.cookie.split(';');
       for(var i=0;i < ca.length;i++) 
       {
        var c = ca[i];
        while (c.charAt(0)==' ') 
        {
         c = c.substring(1,c.length);
         if (c.indexOf(nameEQ) == 0) 
         {
          return c.substring(nameEQ.length,c.length);
         }
        }
       }
       return null;
      }
      </script>
      This set of code should work. It is similar to what danmart has suggested and works if javascript is enabled.

      You may want to consider creating a plugin in Wordpress that allows you to set the option of checking for "upgrade" in individual pages. When you enable the option for a particular page, Wordpress should then include a file that performs cookie checking for on the page.
      {{ DiscussionBoard.errors[2054099].message }}
  • Profile picture of the author Mirnova
    Thanks Frank.
    {{ DiscussionBoard.errors[2054110].message }}

Trending Topics