简体   繁体   English

如何在带有 Crome 或 Microsoft Edge 的本地计算机上使用 localStorage

[英]How to use localStorage on a local machine with Crome or Microsoft Edge

I want to write a simple HTML/js app that is supposed to be used locally.我想编写一个应该在本地使用的简单 HTML/js 应用程序。 It uses localStorage for storing data.它使用localStorage来存储数据。 But I cannot use it because of Crome's and Edge's security politics.但由于 Crome's 和 Edge 的安全政策,我无法使用它。 In Chromium browser it works fine though.不过在 Chromium 浏览器中它运行良好。

So the question is how to let my script work in those browsers?所以问题是如何让我的脚本在这些浏览器中工作? I want to use it on different machines, so using a local web server is not a good solution for me.我想在不同的机器上使用它,所以使用本地 Web 服务器对我来说不是一个好的解决方案。 I am not allowed (and don't want) to install extra software on those machines.我不允许(也不希望)在这些机器上安装额外的软件。

UPD 0. Say we have a file app.html with the following content: UPD 0.假设我们有一个包含以下内容的文件app.html

<script>
  window.onload = function () {
    try {
      localStorage;
    }
    catch (e) {
      console.error("local storage is not available");
      return;
    }
    console.log("this text is never shown in Chrome and Edge");
  }
</script>

Then we open it with a browser, hit F12, choose js console and see results.然后我们用浏览器打开它,按F12,选择js控制台,看看结果。 I'd like not to see any error message.我不想看到任何错误消息。

UPD 1. Saing "locally" I meant that file:// protocol was used. UPD 1. 说“本地”我的意思是使用了file://协议。 Not a local web server.不是本地网络服务器。

But I cannot use it because of Crome's and Edge's security politics但我不能使用它,因为 Crome's 和 Edge 的安全政策

Do you mean some Chrome's and Edge's security politics will block you using local Storage?您的意思是某些 Chrome 和 Edge 的安全策略会阻止您使用本地存储吗? If that is the case, please explain more details about it.如果是这种情况,请解释有关它的更多详细信息。

To use Local Storage in Microsoft Browser, we should not block the browser to use cookies.要在 Microsoft 浏览器中使用本地存储,我们不应阻止浏览器使用 cookie。 Please refer to the following steps to enable it:请参考以下步骤启用:

  1. Click on the three-dot More Actions button in the top-right corner of your window.单击窗口右上角的三点“更多操作”按钮。
  2. Select “Settings” from the menu.从菜单中选择“设置”。
  3. Click “View advanced settings”.单击“查看高级设置”。
  4. Under the “Cookies” section, use the drop menu to select “Don't block cookies”.在“Cookie”部分下,使用下拉菜单选择“不要阻止 Cookie”。
  5. Restart your browser.重新启动浏览器。

More detail steps to enable LocalStorage, please check this article or this link .启用 LocalStorage 的更多详细步骤,请查看本文此链接

Edit:编辑:

Please refer to the following code to use LocalStorage:使用LocalStorage请参考以下代码:

    window.onload = function () {
        try {
            //check if Browser support storage.
            if (typeof (Storage) !== "undefined") {
                // Code for localStorage/sessionStorage.
                //check if localStorage contains the clickcount
                if (localStorage.clickcount) {
                    //if contains the clickcount key, modify its value.
                    localStorage.clickcount = Number(localStorage.clickcount) + 1;
                } else {
                    //else set the default value.
                    localStorage.clickcount = 1;
                } 
                console.log(localStorage.clickcount);

            } else {
                // Sorry! No Web Storage support..
                console.log("Sorry! This version of Browser not support LocalStorage")
            }
        }
        catch (e) {
            console.error("local storage is not available");
            return;
        }
        console.log("this text is never shown in Chrome and Edge");
    }

More detail information, please check this tutorial .更多详细信息,请查看本教程

Edit 2:编辑2:

Either the localStorage or sessionStorage is specific to the protocol of the page . localStorage 或 sessionStorage 特定于页面的协议 If we use the file:// protocol (if the origin uses the file: or data: scheme), it will cause the SecurityError, so the LocalStorage not working.如果我们使用file://协议(如果源使用 file: 或 data: 方案),则会导致 SecurityError,因此 LocalStorage 无法正常工作。

So please host your web page to the web server (such as: IIS), then access it.所以请将您的网页托管到网络服务器(如:IIS),然后访问它。

More detail information, please check the Window.localStorage .更多详细信息,请查看Window.localStorage

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

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