Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Help with a small javascript cookie create/read script?


ghost's Avatar
0 0

I have written a small script that , when a button is clicked, allows the user to input a name, value and number of days until the cookie has expired using prompt popups. The input works, my problem arises when i click the read button. It should displa an alert with the value of the cookie except nothing happens. I have looked for quite a while and cannot find what hte problem is.

Here is the script:

<body> <input type = button value = "Create Cookie" onclick = "getcookiedata()"> <input type = button value = "Read Cookie" onclick = "readcookie(name)"> </body>

<script>

function getcookiedata(){ name = prompt("Enter the cookie name"); value = prompt("Enter the cookie value"); expires = prompt("Enter the number of days until it expires"); createCookie(name, value, expires); }

function createCookie(name, value, expires){ document.cookie = name+"="+value; expires = expires; }

function readcookie(name){ var cookiename = name + "="; cookie_array = document.cookie.split(';'); for(i = 0; i < cookie_array.length; i++){ cookie = cookie_array[i]; while(cookie.charAt(0)==' '){ cookie = cookie.substring(1,cookie.length); } if(cookie.indexOf(cookiename) == 0){ alertbox = cookie.substring(cookiename.length, cookie.length); alert(alertbox); } } }

</script>

If this is the wrong place to be pasting code, please let me know. Thanks for any help.


ghost's Avatar
0 0

Use the Error Console in Firefox to see what error is being encountered. Unless you made an error in pasting the JS into the forum, you made a mistake in your "createCookie" function, not the "readCookie" one.