[英]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")'
被視為字符串,並且為true 。
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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.