javascript - 如果 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;}
});
有没有人有任何想法?
谢谢!
解决方案
你可以检查你是否已经添加了这个参数:
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;
}
推荐阅读
- python - “ManyToManyDescriptor”对象没有属性“过滤器”
- google-cloud-platform - Airflow - Subdag 卡在运行状态,其内部任务成功结束
- c++ - g ++说它无法加载存在的库
- java - RestTemplate ,有没有办法在检查响应正文后重试成功的请求?
- yii2 - 为什么它会给出错误“缺少必需的参数:id”?
- sql - 如何使用 SUBQUERIES 找到每个部门的最高薪水
- awk - 对于字段中的每个唯一出现,转换不同列中另一个字段中的每个唯一出现
- python - Python - 我可以将票分配给具有 2 个列表的员工吗?
- angular - 带有授权标头的 Websocket 连接
- python - 如何在下拉菜单中设置if条件