Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第461天 sessionStrorage也可以使用onstorage事件吗? #2669

Open
haizhilin2013 opened this issue Jul 19, 2020 · 3 comments
Open
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第461天 sessionStrorage也可以使用onstorage事件吗?

3+1官网

我也要出题

@haizhilin2013 haizhilin2013 added the js JavaScript label Jul 19, 2020
@zhaofeipeter
Copy link

zhaofeipeter commented Jul 21, 2020

@Sun-Y0n9
Copy link

Sun-Y0n9 commented Jul 21, 2020

  • 用以下代码测试, localStorage的变化可以监测到,sessionStorage的变化没有监测到.
  • 在浏览器两个标签打开文件, 其中一个点击设置, 查看另一个标签控制台的输出.
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<meta name="renderer" content="webkit">
	<title>Document</title>
	<style>
		button {
			width: 200px;
			height: 40px;
			border: none;
			background-color: aqua;
		}
	</style>
</head>
<body>
	<button id='local'>设置localStorage</button>
	<button id='session'>设置sessionStorage</button>
</body>
</html>
<script type="text/javascript">
	window.onstorage = function (e) {
		console.log(e.key + ' 键已经从 ' + e.oldValue + ' 改变为 ' + e.newValue + '.');
	};
	document.querySelector('#local').addEventListener('click', () => {
		localStorage.setItem('local', Math.random())
	});
	document.querySelector('#session').addEventListener('click', () => {
		sessionStorage.setItem('session', Math.random())
	});
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

4 participants
@haizhilin2013 @zhaofeipeter @Sun-Y0n9 and others