Help Me With Cookies, HTML and IE8

xarzu's picture

They have: 25 posts

Joined: May 2007

Help Me With Cookies, HTML and IE8

For complex reasons, I have to use HTML with JavaScript to set a cookie. I know it is easy to set and get cookies PHP but I have to put the code in HTML.

I have searched the internet for some examples and I ran a test based on an example. I ran this Javascript code:

<SCRIPT LANGUAGE="JavaScript">

function putCookie()
{
cookie_name = "specialcookiehuge";

if(document.cookie != document.cookie)
{
index = document.cookie.indexOf(cookie_name);
}
else
{
index = -1;
}

if (index == -1)
{
document.cookie=cookie_name+"; expires=Monday, 04-Apr-2020 05:00:00 GMT";
}

}
</SCRIPT>

I tested this code by placing alert statements and I nkow that the line:
document.cookie=cookie_name+"; expires=Monday, 04-Apr-2020 05:00:00 GMT";

gets run.

I tried to find this cookie (Tools -> Internet Options -> Browsing History ->Settings) And I did not find anything named "specialcookiehuge" in "View Objects" or "View Files".

I guess I could try other browsers, but it would be good to know what I am doing wrong. I do not know if I am really setting a cookie. Is there some additional javascript command I am missing? Or, is the cookie being set but I am not looking in the right place according to the tutorials I saw online?

JeevesBond's picture

He has: 3,955 posts

Joined: Jun 2002

This code doesn't seem to make much sense. For example:

if(document.cookie != document.cookie)
{
[...]
}

will always be false, maybe there's some Javascript trickery I'm not understanding, but this looks equivalent to:
if(true != true) {
[...]
}

document.cookie=cookie_name+"; expires=Monday, 04-Apr-2020 05:00:00 GMT";

also won't work as you expect, because it's not a name/value pair.

Try this as your script:

<script type="text/javascript">
<![CDATA[//><!--
function putCookie() {
  cookie_name = "specialcookiehuge";
  cookie_value = "specialcookievalue";
  document.cookie = cookie_name + "=" + cookie_value + "; expires=Monday, 04-Apr-2020 05:00:00 GMT; path=/";
}
//--><!]]>
</script>

I haven't checked in IE, but in Google Chrome you can go to Tools -> Javascript console, run the code that's inside the function, then enter document.cookie and you'll see your key/value pair is set.

You will also want to change those local variables into arguments of the function.

a Padded Cell our articles site!

xarzu's picture

They have: 25 posts

Joined: May 2007

This feature with Google Chrome looks very interesting and this is the first time I have seen it. When I click on Tools -> javascript console, it opens up a window at the bottom of the browser that is cut in two. The top half shows the source for the current page. The bottom half shows an entry field. When I past the script you mentioned above, It says (in red) SyntaxError: Unexpected token <

I assume that this must not be how this is used. I assume I need to put this script into an html file and then load that file into chrome and then use this feature to see the results. Am I right?

JeevesBond's picture

He has: 3,955 posts

Joined: Jun 2002

I assume I need to put this script into an html file and then load that file into chrome and then use this feature to see the results. Am I right?

You just open it and start entering Javascript immediately. Don't put <script> tags into it, since they're not Javascript but HTML.

For example, open the Javascript console and paste:

alert('hello world')

Then press enter.

What I meant above, when I said you should copy and paste into the Javascript console, was you should copy and paste:

cookie_name = "specialcookiehuge";
cookie_value = "specialcookievalue";
document.cookie = cookie_name + "=" + cookie_value + "; expires=Monday, 04-Apr-2020 05:00:00 GMT; path=/";

Then copy and paste:

document.cookie

Hope this makes sense! Smiling

a Padded Cell our articles site!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.