如何访问 javascript 中另一个 window 上的本地存储
How to access localstorage on another window in javascript
我正在使用 users.php 页面,所以每当我点击每个用户名前面的 link view_details.php 时:
1) 它将使用该用户 ID 创建 localstorage,每隔一秒 localstorage 将检查该用户 id(是否为空),然后打开新的 window view_details.php
2) 在 view_details.php 中完成更改后,我将在 table 中更新并从此 window 中清除本地存储(查看 details.php)。
3) 因为 setinterval 函数在父 window(user.php) 中是 运行 但是当我们从 view_details.php 中清除它时现在父 window 应该显示一些警报表明该用户的本地存储已清除
但我无法访问在 user.php
中创建的 view_details.php 中的本地存储
user.php
<script>
$("body").on('click', '.view_user_details', function () {
var user_id= $(this).attr("data-id");
window.localStorage.setItem(user_key, user_id); // localstorage is created here with user id
check_user_status(); // function called to check localstorage is cleared or not
//open view_details.php in new window
}
function check_user_status() {
setInterval(function () {
if (localStorage.length > 0) {
console.log('user value:' + window.localStorage.getItem('user_key'));
} else {
console.log('local storage clear');
}
}, 3000);
}
</script>
视图脚本 details.php
<script>
$(".save_details").click(function () {
localStorage.clear();
window.close();
});
</script>
尝试用引号包裹密钥。
window.localStorage.setItem(user_key, user_id);
到
window.localStorage.setItem('user_key', user_id);
我正在使用 users.php 页面,所以每当我点击每个用户名前面的 link view_details.php 时:
1) 它将使用该用户 ID 创建 localstorage,每隔一秒 localstorage 将检查该用户 id(是否为空),然后打开新的 window view_details.php
2) 在 view_details.php 中完成更改后,我将在 table 中更新并从此 window 中清除本地存储(查看 details.php)。
3) 因为 setinterval 函数在父 window(user.php) 中是 运行 但是当我们从 view_details.php 中清除它时现在父 window 应该显示一些警报表明该用户的本地存储已清除
但我无法访问在 user.php
中创建的 view_details.php 中的本地存储user.php
<script>
$("body").on('click', '.view_user_details', function () {
var user_id= $(this).attr("data-id");
window.localStorage.setItem(user_key, user_id); // localstorage is created here with user id
check_user_status(); // function called to check localstorage is cleared or not
//open view_details.php in new window
}
function check_user_status() {
setInterval(function () {
if (localStorage.length > 0) {
console.log('user value:' + window.localStorage.getItem('user_key'));
} else {
console.log('local storage clear');
}
}, 3000);
}
</script>
视图脚本 details.php
<script>
$(".save_details").click(function () {
localStorage.clear();
window.close();
});
</script>
尝试用引号包裹密钥。
window.localStorage.setItem(user_key, user_id);
到
window.localStorage.setItem('user_key', user_id);