简体   繁体   English

存储事件未在 Chrome 的本地 html 文件中触发

[英]Storage event not triggering in local html file in Chrome

I have the following html on a local file at ~/test.html:我在 ~/test.html 的本地文件中有以下 html:

<html>
  <head>
    <title>Test Event</title>
  </head>
  <body>
    Test Event
  </body>
</html>

I open it in two different windows in Chrome (Version 62.0.3202.75).我在 Chrome(版本 62.0.3202.75)的两个不同窗口中打开它。 In the console of the first window I enter the following JS:在第一个窗口的控制台中,我输入以下 JS:

window.addEventListener('storage', function(e) {  
    console.log('criou sdasd');
    console.log(e.key);
    console.log(e.oldValue);
    console.log(e.newValue);
    console.log(e.url);
    console.log(e.storageArea);
});

In the console of the second window I enter the following JS:在第二个窗口的控制台中,我输入以下 JS:

localStorage.setItem('Chronometer.time', '00:00');

And nothing happens in the first window.在第一个窗口中什么也没有发生。

Interesting facts:有趣的事实:

  • If I run localStorage.getItem('Chronometer.time') on the console of the first window I get "00:00" .如果我在第一个窗口的控制台上运行localStorage.getItem('Chronometer.time')我得到"00:00"
  • If I just open two new empty Chrome tabs and do the same steps above, I do get an event triggered in the first window.如果我只是打开两个新的空 Chrome 标签页并执行上述相同的步骤,我会在第一个窗口中触发一个事件。

Do storage events work on local html files?存储事件是否适用于本地 html 文件?

Chrome prevents local files ( file:///* ) from "talking" to each other by default as a security measure.默认情况下,作为安全措施,Chrome 会阻止本地文件 ( file:///* ) 相互“交谈”。 You can disable this restriction by launching Chrome with the --allow-file-access-from-files flag:您可以通过使用--allow-file-access-from-files标志启动 Chrome 来禁用此限制:

chrome.exe --allow-file-access-from-files

Now the storage event should work as expected with local files.现在存储事件应该与本地文件按预期工作。

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

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