首页 > 解决方案 > 如果 url 是某个路径,则添加参数

问题描述

我有一个脚本正在运行,如果有某个 URL,那么这将添加一个参数。例如,如果这个 url 子文件夹有 /de in,那么添加 ?_sft_language=german。如果 /sp 然后添加 ?_sft_language=spanish。我已经用 JavaScript 编写了以下代码,但它运行了多次,但我只希望它运行一次。

$(document).ready(function() {
    if(!window.location.href.match('_sft_language') && window.location.href.match('testsite.com/de/partner/')){var url = document.location.href+"?_sft_language=german";document.location = url;}   

    if(!window.location.href.match('_sft_language') && window.location.href.match('testsite.com/da/partner/')){var url = document.location.href+"?_sft_language=danish";document.location = url;}   
    if(!window.location.href.match('_sft_language') && window.location.href.match('testsite.com/nl/partner/')){var url = document.location.href+"?_sft_language=dutch";document.location = url;}    
    if(!window.location.href.match('_sft_language') && window.location.href.match('testsite.com/fr/partner/')){var url = document.location.href+"?_sft_language=french";document.location = url;}   
    if(!window.location.href.match('_sft_language') && window.location.href.match('testsite.com/it/partner/')){var url = document.location.href+"?_sft_language=italian";document.location = url;}  
    if(!window.location.href.match('_sft_language') && window.location.href.match('testsite.com/ja/partner/')){var url = document.location.href+"?_sft_language=japanese";document.location = url;} 
    if(!window.location.href.match('_sft_language') && window.location.href.match('testsite.com/ko/partner/')){var url = document.location.href+"?_sft_language=korean";document.location = url;}   
    if(!window.location.href.match('_sft_language') && window.location.href.match('testsite.com/no/partner/')){var url = document.location.href+"?_sft_language=norwegian";document.location = url;}    
    if(!window.location.href.match('_sft_language') && window.location.href.match('testsite.com/es/partner/')){var url = document.location.href+"?_sft_language=spanish";document.location = url;}  

    if(!window.location.href.match('_sft_language') && window.location.href.match('testsite.com/partner/')){var url = document.location.href+"?_sft_language=english";document.location = url;} 


}); 

有没有人有任何想法?

谢谢!

标签: javascripthtmljquery

解决方案


你可以检查你是否已经添加了这个参数:

if(!window.location.href.match('_sft_language') && window.location.href.match('testingsite.com/de/partner')){
        var url = document.location.href+"?_sft_language=german";
        document.location = url;

}

推荐阅读