admin管理员组

文章数量:1429953

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"
    ".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.

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/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.

Share Improve this question edited May 22, 2013 at 20:53 user229044 240k41 gold badges344 silver badges347 bronze badges asked May 22, 2013 at 20:49 crackmiggcrackmigg 5,9212 gold badges33 silver badges41 bronze badges 6
  • it was ADDED in HTML5, and to browsers that support HTML5 no matter what document they are rendering, pat and security restrictions aside. HTML5 elements and APIs don't care about the doctype... – dandavis Commented May 22, 2013 at 20:53
  • Web browsers are very forgiving. – j08691 Commented May 22, 2013 at 20:54
  • Out of curiosity, how were you going to add support if local storage isn't supported? – user229044 Commented May 22, 2013 at 20:54
  • @meagar I think the only possible way(without plugins or something) would be cookies – MofX Commented May 22, 2013 at 20:55
  • HTML4 specification does not say that localstorage should not be available. – JJJ Commented May 22, 2013 at 20:56
 |  Show 1 more ment

2 Answers 2

Reset to default 7

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 patible 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./web-toolkit/doc/latest/DevGuideHtml5Storage

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.

本文标签: javascriptLocalStorage on HTML 4 website in modern browsersStack Overflow