This Truly is a Cookie Monster!

by Nick Jensen on November 25, 2013

I am starting to see the light at the end of the tunnel. After a few more blog entries, I think I’ll finally be done with JavaScript. You know, a man named Joseph Campbell once said, “Computers are like Old Testament gods: lots of rules and no mercy.” In the past, I have failed to see the wisdom in his words. Now, after three weeks of learning JavaScript, I’m beginning to understand his point. Learning HTML was relatively easy for me because most of the lessons were provided in a context I could identify and understand. Maybe it’s just me, but JavaScript looks and feels more abstract. Line after line of code just sitting there in the void with little or no context. And the codes become so intricate and obscure that you forget what you were trying to learn in the first place. If one space is off, one bracket, one quotation mark, that complex code you were making turns into an incoherent train-wreck of letters, numbers, and punctuation.

Luckily, not all JavaScript concepts cause severe headaches. String objects can be relatively straightforward, partly because they work with HTML concepts. Like HTML tags, JavaScript strings can be used to enhance text in an HTML file. While using strings may be more troublesome than adding tags in an HTML file, it allows the coder to enhance words while working within the JavaScript (or js) file. As with HTML, text can be altered by color, size, and shape. Here’s an example of how you can alter the color of a font:

<script type=”text/javascript”>

var orange_suit = “Never wear an orange suit to a funeral.”;

document.write(orange_suit.fontcolor(“orange”));

</script>

You can change the font to any size you want:

<script type=”text/javascript”>

var small_talk = “Hey! I’m down here!”;

document.write(small_talk.fontsize(2));

</script>

Perhaps you want to create a link:

<script type=”text/javascript”>

var lastc_link = “Click here to join us in LA!”;

var full_link = “lastc_link.link(“http://www.lastc.org”);

document.write(full_link);

</script>

<a href=”http://www.lastc.org”> Click here to join us in LA!</a>

The above examples are just a preview of what you can do with strings. If you want to move on to something more challenging then look no further. Cookies can be very difficult to learn. No, not the kind that we munch on, but the kind that website developers use to help end users store information. Here’s an example of a cookie.

function putCookie() {

if(document.cookie != document.cookie)

{index = document.cookie.indexOf(cookie_name);}

else

{ index = -1;}

if (index == -1)

{

YouEntered=document.cf.cfd.value;

document.cookie=cookie_name+”=”+YouEntered+”; expires=Monday, 04-Apr-2014 05:00:00 GMT”;

}

}

</SCRIPT>

<FORM NAME=”cf”>

Enter Your Name: <INPUT TYPE=”text” NAME=”cfd” size=”12″>

<INPUT TYPE=”button” Value=”Set to Cookie”

onClick=”putCookie()”>

</FORM>

<SCRIPT LANGUAGE=”JavaScript”>

cookie_name = “dataCookie”;

var YouWrote;

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();

if (YouWrote == “dataCookie”)

{YouWrote = “Nothing_Entered”}

</SCRIPT>

</div>

</body>

</html>

Yes, my friends. That bloody juggernaut of a code is, in fact, a cookie. I can rant on about cookies all day (seeing that I’m still trying to learn them), but I already spend enough time ranting….err I mean discussing the complexity of JavaScript codes. Let’s just hope that somewhere out there in the digital world exists a program to help people like myself create cookies. Perhaps Dreamweaver can lend us a hand. Speaking of which, I’ll be learning it after I finish trudging through JavaScript. Oh, how I look forward to a change in pace.

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: