以下代码
$(document).ready(function(){
sessionStorage.test = false;
alert( sessionStorage.test );
if ( sessionStorage.test ) {
alert("I dont care about your conditions!");
}
});
生成以下弹出窗口:
假
我不在乎你的条件!
即使sessionStorage.test
显然设为false。为什么会这样?根据这个答案https://stackoverflow.com/a/17986241/2311074,这应该有效。我究竟做错了什么?我正在使用XAMPP - 它可能与sessionStorage有问题吗?这是我的测试文件的完整代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script >
$(document).ready(function(){
sessionStorage.test = false;
if ( sessionStorage.test ) {
alert("I dont care about your conditions!");
}
});
</script>
</head>
<body>
<!-- page content -->
</body>
</html>
答案 0 :(得分:4)
sessionsStorage的默认getter / setter是:
sessionStorage.setItem('keyName', 'value');
sessionStorage.getItem('keyName')
所以,例如:
sessionStorage.setItem('test',false);
if (sessionStorage.getItem('test') != "false") {
..........
}
重复主题:
- sessionStorage setItem returns true or false
- sessionStorage isn't working as expected