Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 执行后取消js文件的链接_Javascript_Php_Wordpress_Unlink - Fatal编程技术网

Javascript 执行后取消js文件的链接

Javascript 执行后取消js文件的链接,javascript,php,wordpress,unlink,Javascript,Php,Wordpress,Unlink,我想在脚本文件执行后删除它。应将其从所有会话中删除。我在functions.php中为子主题链接了文件: function tutsplus_enqueue_custom_js() { wp_enqueue_script('custom', get_stylesheet_directory_uri().'/scripts/redirect.js'); } 正如我所说的,它应该在整个会话中只执行一次,而不是页面。我知道应该通过取消链接来完成,但我不知道如何编写一个条件,只有在执行该文件时

我想在脚本文件执行后删除它。应将其从所有会话中删除。我在functions.php中为子主题链接了文件:

function tutsplus_enqueue_custom_js() {
    wp_enqueue_script('custom', get_stylesheet_directory_uri().'/scripts/redirect.js');
}
正如我所说的,它应该在整个会话中只执行一次,而不是页面。我知道应该通过
取消链接
来完成,但我不知道如何编写一个条件,只有在执行该文件时才能将其从整个会话中删除。你能帮我吗

新脚本:

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);
}
let executed=window.sessionStorage.getItem(“sessionDecexed”)
console.log(已执行)
如果(已执行!=1){
让uri=window.location;
让lang=window.navigator.language;
if(uri.href.indexOf('lang')<0){
如果((lang!='ru')&&(lang!='ru')){
eng=window.location.href.includes('https://www.site/en/');
如果(eng==false){
让re=”https://www.site/";
让url=window.location.href;
让newstr=url.replace(re,'https://www.site/en/');
newstr+=“?货币=美元”;
window.location.href=newstr;
}
}
}
setItem(“SessionDeecExecuted”,1);
}
您可以使用检查脚本是否已执行,并将此信息存储在那里。然后,检查该值是否存在。如果是,请阻止代码在下一页重新加载时执行。请注意,当您关闭并重新打开浏览器时,代码将再次执行(看起来这正是您所需要的)

(函数(){
let executed=window.sessionStorage.getItem(“sessionDecexed”);
如果(!sessionStorage.getItem('SessionDeecExecuted')){
setItem('SessionDeecExecuted','yes');
}
如果(执行)返回;
/*以下脚本在当前浏览器会话期间仅执行一次*/
让uri=window.location;
让lang=window.navigator.language;
if(uri.href.indexOf('lang')<0){
如果((lang!='ru')&&(lang!='ru')){
eng=window.location.href.includes('https://www.site/en/');
如果(eng==false){
让re=”https://www.site/";
让url=window.location.href;
让newstr=url.replace(re,'https://www.site/en/');
newstr+=“?货币=美元”;
window.location.href=newstr;
}
}
}
})();

您可以在添加脚本后设置会话变量。然后可以检查会话变量是否已设置。如果不存在,则加载脚本;如果存在,则不加载脚本。这样,它将只添加到每个会话的第一个页面加载中。如果您指的是PHP命令
unlink()
,那么将从系统中完全删除该文件,这可能不是您想要的?@MagnusEriksson我正在使用wordpress。我不知道会话变量在哪里。我是否可以仅为当前用户而不是从系统中删除文件?@MagnusEriksson I edited functions.php。我在Begging中添加了这一点:
add_action('wp_enqueue_scripts','tutsplus_enqueue_custom_js');函数tutsplus_enqueue_custom_js(){wp_enqueue_script('custom',get_stylesheet_directory_uri()。/scripts/redirect.js');}如果(!session_id()){session start()}
没有任何更改,为什么不使用sessionstore呢?您可以在第一页加载时在那里存储一个值,并在js文件
redirect.js
的开头进行检查,以便在sessionStorage(脚本已执行)中存在值时尽早返回。@Badan我如您所说编辑了问题(添加了脚本)
console.log
写入
1
,但脚本仍在运行。怎么了?我的问题中有类似的代码,它们都不能阻止第二次重定向。它保持重定向让我测试一下,我可能犯了一个错误。我刚刚测试了代码,它确实将值存储在
sessionStorage
中,如果在
if(executed)return之后添加
console.log('here')
行,您将看到它不会在第二次重新加载页面时打印任何内容。请注意,它仍然会在每次页面加载时运行脚本,直到到达return语句为止。您能清除浏览器缓存并重试吗?我添加了
console.log('here')
,它实际上只显示了一次,但每次脚本都在运行(即使我重新加载页面并尝试更改语言)。在那之前我清除了缓存。我忘了为
eng
添加
let
,但是当我添加时,没有任何变化
(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;
          }

      }
  }
})();