April 10, 200818 yr Hello, I'm trying to use a form which allows a user to enter their name. This is then stored in a cookie and used to personalise the page by showing their name. I can't get the code to work though as when I click getCookie() the value isn't showing up. I'm just trying to get the cookie to show in an alert box for now then I'll try and get to print straight onto the page. Actually what would be the best way to do that? I dont think you can print stuff to form labels so it shows in a transparent box? Anyway heres the code I have so far and javascripts arent disabled on firefox: <script type="text/javascript"> function setcookie() { var the_cookie = escape(custLogin.value); document.cookie = the_cookie; alert("cookie set: " + the_cookie); } function getcookie() { var cook=document.cookie; alert("cookie got: "+cook); } </script> <div id = "login" class = "login"> <form action = "" method = ""> <h3><label> Customer Login</label></h3> <label class = "label" id = "label"> Name: </label><input name="custLogin" type = "text" id="custLogin" size = "16" maxlength = "30"/><br/> <input name = "Submit" type = "submit" value = "Submit" class = "submit" id = "submit" onclick="setcookie()"/> </form> </div> <button onclick= "getcookie()">Get cookie</button> Thanks!
April 11, 200818 yr Author Should have posted the URL Its something to do with firefox. It works in IE6. Javascript isnt disabled in FF though.
April 11, 200818 yr Should have posted the URL Its something to do with firefox. It works in IE6. Javascript isnt disabled in FF though. IE6 imports all ID'd elements to the global namespace. What this means is that you can access the login field from anywhere in your JavaScript by using the variable custLogin. Standards-conforming browsers don't do that. What you want to do is: custLogin = document.getElementById('custLogin') Also, cookies have a specific syntax. You'll probably want to grab a function for setting and reading cookies - from here for example - to make sure they're stored properly.
Create an account or sign in to comment