error using sessionStorage

2019-07-02 06:42发布

I am trying to use sessionStorage on a page but it is giving me an error. Here is the code:

<script type="text/JavaScript">
 sessionStorage.setItem("classname", "value1");
 sessionStorage.setItem("classdesc", "value2");
</script>

The error I get from firebug console is: Operation is not supported" code: "9

When I try to use localStorage it works fine, so why does sessionStorage cause an error?

1条回答
够拽才男人
2楼-- · 2019-07-02 07:44

The only thing I can think of is that "the page isn't ready", try placing this code at the end of the body section

<html>
  <head></head>
  <body>
    <!-- YOUR PAGE HERE -->
    <script type="text/JavaScript">
      sessionStorage.setItem("classname", "value1");
      sessionStorage.setItem("classdesc", "value2");
    </script>
  </body>
</html>

Final Answer: sessionStorage wont work in "local mode", but it will work if you upload the file to a server.

查看更多
登录 后发表回答