执行后取消链接js文件
Unlink js file after execution
我想在脚本文件执行后删除它。它应该从所有会话中删除。我在 functions.php 中为子主题链接了文件:
function tutsplus_enqueue_custom_js() {
wp_enqueue_script('custom', get_stylesheet_directory_uri().'/scripts/redirect.js');
}
正如我所说 - 它应该在整个会话中只执行一次,而不是页面。我知道它应该用 unlink
完成,但我不知道如何编写一个条件,仅在执行该文件时才将其从整个会话中删除。你能帮帮我吗?
新脚本:
let executed = window.sessionStorage.getItem("sessionCodeExecuted")
console.log(executed)
if (executed != 1) {
let uri = window.location;
let lang = window.navigator.language;
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);
}
您可以使用 sessionStorage 检查脚本是否已执行并将此信息存储在那里。然后,检查该值是否存在。如果是,请阻止代码在下一页重新加载时执行。请注意,当您关闭并重新打开浏览器时,代码将再次执行(看来这正是您所需要的)。
(function(){
let executed = window.sessionStorage.getItem("sessionCodeExecuted");
if (!sessionStorage.getItem('sessionCodeExecuted')) {
sessionStorage.setItem('sessionCodeExecuted', 'yes');
}
if (executed) return;
/* below script will execute only once during the current browser session. */
let uri = window.location;
let lang = window.navigator.language;
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;
}
}
}
})();
我想在脚本文件执行后删除它。它应该从所有会话中删除。我在 functions.php 中为子主题链接了文件:
function tutsplus_enqueue_custom_js() {
wp_enqueue_script('custom', get_stylesheet_directory_uri().'/scripts/redirect.js');
}
正如我所说 - 它应该在整个会话中只执行一次,而不是页面。我知道它应该用 unlink
完成,但我不知道如何编写一个条件,仅在执行该文件时才将其从整个会话中删除。你能帮帮我吗?
新脚本:
let executed = window.sessionStorage.getItem("sessionCodeExecuted")
console.log(executed)
if (executed != 1) {
let uri = window.location;
let lang = window.navigator.language;
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);
}
您可以使用 sessionStorage 检查脚本是否已执行并将此信息存储在那里。然后,检查该值是否存在。如果是,请阻止代码在下一页重新加载时执行。请注意,当您关闭并重新打开浏览器时,代码将再次执行(看来这正是您所需要的)。
(function(){
let executed = window.sessionStorage.getItem("sessionCodeExecuted");
if (!sessionStorage.getItem('sessionCodeExecuted')) {
sessionStorage.setItem('sessionCodeExecuted', 'yes');
}
if (executed) return;
/* below script will execute only once during the current browser session. */
let uri = window.location;
let lang = window.navigator.language;
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;
}
}
}
})();