【发布时间】:2021-07-16 17:56:50
【问题描述】:
我有这个脚本的 JS 文件:
(我编辑了我的脚本并添加了sessionStorage检查)
let executed = window.sessionStorage.getItem("sessionCodeExecuted")
console.log(executed)
let uri = window.location;
let lang = window.navigator.language;
if (executed != 1) {
if( uri.href.indexOf('lang') < 0) {
if ((lang != 'ru-RU') && (lang != 'ru')){
eng = window.location.href.includes('https://www.site/en/');
if (eng == false){
let re = "https://www.site/";
let url = window.location.href;
let newstr = url.replace(re, 'https://www.site/en/');
newstr += "?currency=USD";
window.location.href = newstr;
}
}
}
window.sessionStorage.setItem("sessionCodeExecuted", 1);
}
我只想在每个会话中运行一次。我在functions.php中开始使用这段代码:
if (!session_id()) {
session_start();
}
console.log 写入 1 但脚本仍在运行。怎么了?
【问题讨论】:
-
您到底遇到了什么问题?如果您想基于 PHP 会话限制此代码,则在会话中存储一条信息,告诉它是否已经运行并使用 AJAX 与客户端通信。
-
如果你想在浏览器的一个会话中运行代码,你可以为此创建一个cookie,值为0。这意味着当用户结束会话时cookie将被删除浏览器。检查此 cookie 是否存在
标签: javascript php wordpress session