www.webdeveloper.com
Results 1 to 2 of 2

Thread: real quick if else cookie statement

  1. #1
    Join Date
    Sep 2003
    Location
    Portsmouth, UK Occupation: Web Developer
    Posts
    2,602

    real quick if else cookie statement

    hi i cant figure this out for the life of me

    <script language="JavaScript">

    cookie_name = "dataCookie";

    function getName() {
    if(document.cookie)
    {
    index = document.cookie.indexOf(cookie_name);
    if (index != -1)
    {
    namestart = (document.cookie.indexOf("=", index) + 1);
    nameend = document.cookie.indexOf(";", index);
    if (nameend == -1) {nameend = document.cookie.length;}
    YouWrote = document.cookie.substring(namestart, nameend);
    return YouWrote;
    }
    }
    }

    YouWrote=getName();
    document.write(YouWrote);
    </script>

    if the cookie doesnt exist then YouWrote = undefined is there a way to get round this, i know this is easy in php but dont ask

    if(isset($_COOKIE["name")) {
    echo $_COOKIE["name"];
    } else {
    //do nothing or echo something else
    }

    can anybody help me? thanks
    Rich
    Richard Turner - A Virtual Insight

  2. #2
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    Why don't you just do this?
    Code:
    function getName() {
    
    var YouWrote="";
    
    if(document.cookie)
    {
    index = document.cookie.indexOf(cookie_name);
    if (index != -1)
    {
    namestart = (document.cookie.indexOf("=", index) + 1);
    nameend = document.cookie.indexOf(";", index);
    if (nameend == -1) {nameend = document.cookie.length;}
    YouWrote = document.cookie.substring(namestart, nameend);
    }
    }
    
    return YouWrote;
    
    }
    Every fight is a food fight when you’re a cannibal.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles