Javascript 如果url是某个路径,则添加参数
我有一个脚本运行,如果有一个特定的URL,那么这将添加一个参数。例如,如果此url子文件夹中有/de,则添加?\u sft\u language=德语。如果/sp,则添加?\u sft\u语言=西班牙语。我已经用JavaScript编写了下面的代码,但它运行了多次,但我只希望它运行一次Javascript 如果url是某个路径,则添加参数,javascript,html,jquery,Javascript,Html,Jquery,我有一个脚本运行,如果有一个特定的URL,那么这将添加一个参数。例如,如果此url子文件夹中有/de,则添加?\u sft\u language=德语。如果/sp,则添加?\u sft\u语言=西班牙语。我已经用JavaScript编写了下面的代码,但它运行了多次,但我只希望它运行一次 $(document).ready(function() { if(!window.location.href.match('_sft_language') && window.locat
$(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;}
});
有人有什么想法吗
谢谢大家! 您可以检查是否已添加此参数:
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;
}
大家好,在我的脚本中是这样的:-if(!window.location.href.match('u sft_language')&&window.location.href.match('testsite.com/de/partner'){var url=document.location.href+“?\u sft_language=德语”;document.location=url;}if(!window.location.href.match('u sft_language')&&window.location.href.match)('testsite.com/da/partner'){var url=document.location.href+“?\u sft\u language=danish”;document.location=url;}Hi@Malarres我已经编辑了我的答案,以显示我在做什么。但是仍然无法工作,因为它需要覆盖以前的参数。如果编辑后代码仍在运行多次,我想不通:(可能与代码库中的其他部分有关?除非手动触发ready事件,否则此代码不会执行多次。通常,ready事件仅在加载DOM后执行。