[英]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:有趣的事实:
localStorage.getItem('Chronometer.time')
on the console of the first window I get "00:00" .如果我在第一个窗口的控制台上运行localStorage.getItem('Chronometer.time')
我得到"00:00" 。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.