关于"的脚本问题链接重定向"

时间:2017-04-04 05:33:42

标签: javascript php css html5

这是脚本:

<script>
if(document.location.href.indexOf('https://thedomain.com/collections/all?sort_by=best-selling') > -1) { 
document.location.href = 'https://thedomain.com/pages/bestsellers';
}
</script>

问题是,我如何制作代码,以便在我放置https://thedomain.com/////////////////////////////////////////collections/all?sort_by=best-selling

它仍会将我发送到指定的链接。或者每当我复制任何&#34; /&#34; &#34;&#34; &#34; _&#34; &#34; =&#34; &#34; - &#34;字符。当我在我的网站上复制这些字符时,它不会重定向到我希望它去的页面(或者它不会自动执行)

底线是我不想被迫这样做(效率低下):

<script>
if(document.location.href.indexOf('https://thedomain.com/////////////////////////////////////////collections/all?sort_by=best-selling') > -1) { 
document.location.href = 'https://thedomain.com/pages/bestsellers';
}
</script>

1 个答案:

答案 0 :(得分:1)

只需使用以下注册表从网址中删除额外的斜杠:

var correctURL= document.location.href.replace(/([^:]\/)\/+/g, "$1"); 
//removes every slash that follows after a character that is not : and then a slash.

现在您可以针对此网址进行验证:

if(correctURL.indexOf('https://thedomain.com/collections/all?sort_by=best-selling') > -1) { 
    document.location.href = 'https://thedomain.com/pages/bestsellers';
}