Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.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 将URL拆分为数组,然后根据语言更改URL_Javascript_Jquery - Fatal编程技术网

Javascript 将URL拆分为数组,然后根据语言更改URL

Javascript 将URL拆分为数组,然后根据语言更改URL,javascript,jquery,Javascript,Jquery,我希望改变网址,这取决于网站是否加载了不同的语言。我想我很接近了 检查站点是否为just.com(如果是这种情况,请别管它)或.com/fr/slug.com/de/slug.com/es/slug是否变成.com/fr.com/de 英语网站。只有.com/slug的是.com//slug。想解决这个问题吗 // take url and put into array var pathArray = window.location.pathname.split('/');

我希望改变网址,这取决于网站是否加载了不同的语言。我想我很接近了

检查站点是否为just.com(如果是这种情况,请别管它)或.com/fr/slug.com/de/slug.com/es/slug是否变成.com/fr.com/de

英语网站。只有.com/slug的是.com//slug。想解决这个问题吗

   // take url and put into array
    var pathArray = window.location.pathname.split('/');
    var newPathname = "";
    for (i = 0; i < pathArray.length; i++) {
      // if statement to prevent extra '/' at end of url
      if (i < 2 ){
        newPathname += pathArray[i] + "/";
      }
    }
    newPathname.toString();

    if (newPathname == "/de/" || "/fr/" || "/es/" || "/ja/" || "/zh-hant/"){
      var newURL = window.location.host + newPathname;
    console.log(newURL);
    document.querySelector(".navbar__logo__wrapper").href = newURL;
    }
    }

//获取url并放入数组
var pathArray=window.location.pathname.split('/');
var newPathname=“”;
对于(i=0;i
navbar\uuuuu logo\uuuu img的元素是什么?天哪,我选错了元素。事实上,它是导航栏的标志包装。仍然不是100%工作,但这是一个标签什么不是100%工作?如果它是标签,那么它应该正确分配href。当然你的服务器可以为你这样做,为什么客户端要这样做?navbar\uuuuu logo\uuuuu img的元素是什么?天哪,我选择了错误的元素。事实上,它是导航栏的标志包装。仍然不是100%工作,但这是一个标签什么不是100%工作?如果它是标签,那么它应该正确地分配href。当然你的服务器可以为你这样做,为什么要在客户端这样做?