Login | Register | FAQ
Anonymous

difficulties running javascript

+ Post a reply

2 posts Page 1 of 1


difficulties running javascript

by tareek11 » Thu Mar 06, 2014 3:15 pm

For lesson 15_Get myCookie, I typed the code in my editor, tried to run it, but nothing happens.

I checked each line of the code to make sure there was not a typo, and still nothing happens.

Can someone assist?

Ps. I have provided the code below so that you do not have to search.

Thanks


<!DOCTYPE html>
<html>
<head>
<title>Lesson 15: Get myCookie</title>
</head>
<body>
<h1>Lesson 15: Get myCookie</h1>

<script type="text/javascript">

//continue from the previous code snippet

else

{

var myCookieString = unescape(document.cookie);

//create a comma-seperated array of subsctrings from the string

//comntaining the cookie values by using

//split() with "=" as separator.

//Remember name/value pairs in the cookie

//string are seperated by an = sign:

var dataList = myCookieString.split("=");

//check that the name at index position 0 is

//"myCookie" (remember the cookie name from previous example):

if (dataList[0] === "myCookie")

{

//let's extract the value of myCookie

//by using split() once again. Now the items in

//dataList array are seperated by " , ".

//This will be our separator in the split() method.

//Item 1 (index 0) is the cookie name,

//item 2 (index 1) is the value, which is what we need:

var data = dataList[1].split(",");

//Now we have a new array of strings called data.

//containing only the cookie value ("Test Cookie"),

//which is at index 0. We extract it:

var cookieName = data[0];

}

//Now we display a personalized greeting

document.write("Welcome back, here's your cookie, " + cookieName);

}

</script>

</body>
</html>
User avatar

tareek11

  • Posts: 1
  • Joined: Thu Mar 06, 2014 3:11 pm

Re: difficulties running javascript

by XainPro » Mon Mar 10, 2014 11:39 am

it's mentioned in lesson you should continue from last code the full version of the code will be

Code: Select all
<!DOCTYPE html>
<html>
<head>
<title>Lesson 15: Get myCookie</title>
</head>
<body>
<h1>Lesson 15: Get myCookie</h1>

<script type="text/javascript">
      //If no cookie is present, we create one
      
      if (! document.cookie)
      
      {
      
      var cookieName = "Test Cookie";
      
      //escape the string to make sure there are
      
      //no unallowed characters
      
      var myCookie = escape(cookieName);
      
      //set the expiry date to 1 minute from creation date:
      
      //get current date and add 1 minute reduced to milliseconds:
      
      var expiryDate = new Date();
      
      expiryDate.setTime(expiryDate.getTime() + (60 * 1000));
      
      //create the myCookie cookie and add it
      
      //to the cookie property of the document object:
      
      //in name=value pairs separated by ;
      
      document.cookie = "myCookie=" + myCookie + ";" + "expires=" + expiryDate.toGMTString() + ";";
      
      //leave a message to the user:
      
      document.write("Thank you, " + cookieName + " is your cookie");
      
      }
//continue from the previous code snippet

else

{

var myCookieString = unescape(document.cookie);

//create a comma-seperated array of subsctrings from the string

//comntaining the cookie values by using

//split() with "=" as separator.

//Remember name/value pairs in the cookie

//string are seperated by an = sign:

var dataList = myCookieString.split("=");

//check that the name at index position 0 is

//"myCookie" (remember the cookie name from previous example):

if (dataList[0] === "myCookie")

{

//let's extract the value of myCookie

//by using split() once again. Now the items in

//dataList array are seperated by " , ".

//This will be our separator in the split() method.

//Item 1 (index 0) is the cookie name,

//item 2 (index 1) is the value, which is what we need:

var data = dataList[1].split(",");

//Now we have a new array of strings called data.

//containing only the cookie value ("Test Cookie"),

//which is at index 0. We extract it:

var cookieName = data[0];

}

//Now we display a personalized greeting

document.write("Welcome back, here's your cookie, " + cookieName);

}

</script>

</body>
</html>
User avatar

XainPro

  • Posts: 3933
  • Joined: Fri Feb 17, 2012 8:10 pm


+ Post a reply

Page 1 of 1

cron