如果当前url包含,JS将用户重定向到不同的页面
JS redirect user to different page if the current url contains .
我使用的软件不允许我直接编辑某些页面。如果观众正在查看的页面包含某个字符串,我想将他们重定向到另一个URL。
这是我尝试过的,但它似乎会将你重定向到新的位置,无论你在网站的哪个页面上。
<script>
var EDITURL = window.location.href;
if ('url:contains("testthis")') {
window.location.replace("http://www.test.com");
};
</script>
有什么想法吗?
在JavaScript中,'url:contains("testthis")'
被视为一个字符串,并且是truthy。
if('your text'){
// code here will always be executed.
}
这个代码应该工作:
<script>
if(document.location.href.indexOf('testthis') > -1) {
// indexOf will return the position of the first occurence of this string in the url
// or -1 it it's not there.
document.location.href = 'http://www.test.com';
}
</script>
如我所见:
if(EDITURL.indexOf('testthis') > -1) { ... }
或者同样可读性较差:
if(~EDITURL.indexOf('testthis')) { ... }
关于:
var fullUrlLink = location.href;
if (fullUrlLink.search("/yourelement/") >= 0) {
window.location.replace("http://stackoverflow.com");
} else {
window.location.replace("http://stackoverflow.com");
}
相关文章:
- 如何使用Ajax调用特定的控制器和操作来重定向用户
- 意思是.js - 登录后重定向用户 - 在本地工作,但在部署时不起作用
- 当用户在选择框中选择选项时,使用window.location.href重定向用户只会更改导航器中URL的最后一位
- 如何从特定国家/地区重定向用户
- 注销后如何重定向用户
- 如何在不重定向用户的情况下更改地址栏URL(不使用历史API?)
- 试图在表单post之后使用jQuery.post()重定向用户
- 根据React中的状态重定向用户
- 如何根据 html 中的选项/选择重定向用户(单击按钮后)
- 如果 cookie 为空 jquery,则设置和重定向用户
- 如果用户在发布之前没有设置密码,请重定向用户
- 使用 Javascript 根据设备重定向用户
- 如何删除未定义的对象错误并根据位置重定向用户
- 如何使用 PHP 而不是 JS 重定向用户
- 根据时区重定向用户
- 在 iframe 源更改后重定向用户
- Ionic (AngularJS) - 重定向用户和路由
- 成功提交表单后重定向用户
- 未重定向用户的控制器操作是从AJAX调用的
- 超链接没有't使用jQuery重定向用户