crackmigg
crackmigg

Reputation: 5891

LocalStorage on HTML 4 website in modern browsers?

I was thinking about writing a JS wrapper to have an HTML 5 localStorage object on older websites using HTML 4, because everybody says its only available with HTML 5. So I wrote up this simple page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <script type="text/javascript">
            console.log(localStorage);
        </script>
    </body>
</html>

... and I expected the console to give me an undefined so I can start implementing. But it just shows the local storage object, which I can use normally! I tried it on latest version of Chrome, Safari and Firefox, and it just works.

So my Q is:

Do I miss something or is localStorage usable in modern browsers regardless of the doctype stating HTML4 or HTML5?

Is it just not known that this works? Works for me... but why is everybody talking about HTML 5 being a requirement then? I did not find any source stating this just works in HTML4.

Upvotes: 1

Views: 3088

Answers (2)

Charles
Charles

Reputation: 1

Web Storage was conceived as a part of the HTML 5 specification. Since then, however, both HTML5 and Web Storage are being developed simultaneously as separate standards, neither of which are yet finalized. As a result, Web Storage could work with older versions of HTML, but only modern browsers can support either standard.

Upvotes: 0

David L
David L

Reputation: 33823

The reason that localStorage is attributed to HTML 5 is not because of the HTML 5 doctype, but rather because it requires an HTML 5 compatible BROWSER. It is the browser that determines if the features are present to create and maintain local storage.

Please see the following link for some interesting information regarding client-side storage: https://developers.google.com/web-toolkit/doc/latest/DevGuideHtml5Storage

Upvotes: 7

Related Questions