Jump to content

HTML 5 Cookies and Local Storage


ekuemoah
 Share

Recommended Posts

Hello,I used the localStorage command to store a cookie and retrieve a cookie. I noticed that I was able to retrieve the stored value when I stored the cookie and then pressed retrieve button. However, I was not able to do this when I stored it, closed the browser, and then tried to retrieve it. It acted like a session cookie or a temporary cookie was stored instead of a persistent cookie. How do I make the cookie into a permanent or persistent cookie?Thanksfunction storeMyName(){var fname =document.getElementById('fname').value;alert(fname);localStorage.setItem('firstname',fname);alert("Name Stored");}function getMyName(){ alert(localStorage.getItem('firstname')); }

Link to comment
Share on other sites

Let me first try to give you some medicine for the marketing hype acid you've been drinking...HTML5 is both a buzzword and an actual specification (that's where it's unique to previous hypes)...HTML5 - the spec - defines a few new elements and attributes for the sake of semantics. They don't really "do" much, with a few motable exceptions like the "video" and "audio" elements which have the ambition of complementing "img", and the new values for the "type" attribute of the "input" element. It's major goal is not those elements however, but to standardize how browsers handle non well formed HTML.HTML5 - the buzzword - is an umbrella term for every fancy new unifinished thing browsers are implementing such as the HTML5 spec itself, various CSS3 modules, SVG, JavaScript APIs (such as localStorage, geoLocation, Web Workers and many more), etc.Many developers, myself included, frown upon the use of buzzwords as if they're technical terms. In technical discussions, HTML5 should always refer to HTML5 - the spec.Also, localStorage is not the same as cookies. It's similar in that it provides a way for you to store data per computer/browser, but it's different in that this information is only available to the JavaScript of that computer/browser and has a much greather capacity.OK... enough medicine... for your issue... could you show us the full code? It sounds like there's a third function there that's clearing the storage (probably in an onunload event handler).<< Moved to the JavaScript forum; reasoning above >>

Link to comment
Share on other sites

Can I still use HTML or Javascript cookies in my program alongside with the WebStorage methods. If use cookies and the DOCTYPE is set to HTML 5, will I get an error in my program or will my cookie program functions not work any more?Here's my code:

function storeMyName(){var fname =document.getElementById('fname').value;alert(fname);localStorage.setItem('firstname',fname);alert("Name Stored");}function getMyName(){ alert(localStorage.getItem('firstname')); }

Let me first try to give you some medicine for the marketing hype acid you've been drinking...HTML5 is both a buzzword and an actual specification (that's where it's unique to previous hypes)...HTML5 - the spec - defines a few new elements and attributes for the sake of semantics. They don't really "do" much, with a few motable exceptions like the "video" and "audio" elements which have the ambition of complementing "img", and the new values for the "type" attribute of the "input" element. It's major goal is not those elements however, but to standardize how browsers handle non well formed HTML.HTML5 - the buzzword - is an umbrella term for every fancy new unifinished thing browsers are implementing such as the HTML5 spec itself, various CSS3 modules, SVG, JavaScript APIs (such as localStorage, geoLocation, Web Workers and many more), etc.Many developers, myself included, frown upon the use of buzzwords as if they're technical terms. In technical discussions, HTML5 should always refer to HTML5 - the spec.Also, localStorage is not the same as cookies. It's similar in that it provides a way for you to store data per computer/browser, but it's different in that this information is only available to the JavaScript of that computer/browser and has a much greather capacity.OK... enough medicine... for your issue... could you show us the full code? It sounds like there's a third function there that's clearing the storage (probably in an onunload event handler).<< Moved to the JavaScript forum; reasoning above >>
Link to comment
Share on other sites

Cookies and WebStorage are two separate things, not dependant on each other.WebStorage in dependant on JavaScript... it can be used in any environment that has JavaScript and supports this API (e.g. IE8+, Firefox 3.5, etc.).Cookies are not specific to JavaScript or HTML... they're specific to the HTTP protocol. They can be used by any user agent that supports HTTP and cookies (e.g. all browsers).What you're showing us is not your full code. That's only part of your code. Show us everything from the DOCTYPE to the "</html>".

Link to comment
Share on other sites

  • 10 months later...

I have a question on localstorage as well. When I copy it to Dreamweaver so that from the w3schools.com tutorial so that I have an idea what it is I get the message: there is an error on line5. Code hinting may not work until you fix this error. Here is the code that gives this message:

<script type="text/javascript">if (localStorage.pagecount){localStorage.pagecount=Number(localStorage.pagecount) +1;}else{localStorage.pagecount=1;}document.write("Visits: " + localStorage.pagecount + " time(s).");</script>

line 5 is the one that says:

localStorage.pagecount=Number(localStorage.pagecount) +1;

I do not know enough from just a few days of reading the online tutorial to figure out what the error is.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...