简体   繁体   English

持久性HTML / Javascript菜单的本地存储或会话存储

[英]local storage or session storage for a persistent HTML/Javascript menu

In the past I'vee always maintained a menu for each PHP page allowing me to set the active (selected) item in the list. 在过去,我总是为每个PHP页面维护一个菜单,允许我在列表中设置活动(选中)项。 I think it makes more sense to call include the menu just once maybe in my header script and then set/retrieve the state for each page via javascript. 我认为在我的标题脚本中调用包含菜单一次,然后通过javascript设置/检索每个页面的状态更有意义。

Using local storage would seem to meet this requirement. 使用本地存储似乎符合此要求。 Is it better to use local storage or session storage for this? 为此使用本地存储或会话存储更好吗? It seems as though using local storage would be better for performance. 似乎使用本地存储会更好地提高性能。

Does anyone have any experience with this or have any recommendations/suggestions? 有没有人有这方面的经验或有任何建议/意见?

My current HTML looks like this: 我目前的HTML看起来像这样:

<ul class="nav nav-list">
  <li class='nav-header'>Menu</li>
  <li class="active" id="page-1"><a href="page1.php">Page 1</a></li>
  <li><a href="page2.php" id="page-2">Page 2</a></li>
  <li><a href="page3.php" id="page-3">Page 3</a></li>
</ul>

Can I use the ID and set it in local storage and check it for each page load? 我可以使用该ID并将其设置在本地存储中并检查每个页面加载吗?

There's no performance difference between session and local storage. 会话和本地存储之间没有性能差异。 The difference is in the persistence. 不同之处在于持久性。 Session storage is deleted as soon as the user closes the browser window. 用户关闭浏览器窗口后立即删除会话存储。 Local storage is maintained across multiple visits to the web page. 在多次访问网页时维护本地存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM