仅在url的索引上运行脚本

时间:2015-03-14 15:02:17

标签: javascript jquery

以下是一些脚本的片段,它允许它在包含特定字符串的URL上运行。

if(location.href.indexOf("MODULE=MESSAGE")>0||location.href.indexOf("/message")>0) { //GOOD TO GO
    if(location.href.indexOf("c=0")>0||location.href.indexOf("c=0")>0) chatLinkStyle = " style='display:none'";

我需要为indexOF添加第二个允许的脚本(" SEQNO")

以下是当前允许页面的URL字符串

mysite/2015/55392?MODULE=MESSAGE1
mysite/2015/55392?MODULE=MESSAGE2
mysite/2015/55392?MODULE=MESSAGE3

ECT ..... MESSAGE20

除了当前的

之外,这是我想要允许的其他URL字符串
mysite/2015/options?L=55392&O=247&SEQNO=0
mysite/2015/options?L=55392&O=247&SEQNO=1
mysite/2015/options?L=55392&O=247&SEQNO=2

等... SEQNO = 20

不知道如何编辑上面的内容以允许多个indexOf,请帮忙。

2 个答案:

答案 0 :(得分:1)

只需添加多个if语句(||)

    <script type="text/javascript">
var myurl = "http://yourwebsite.com/index.php";
var currenturl = window.location;
if(myurl == currenturl) {
    //run your code
}
</script>

答案 1 :(得分:0)

更清洁的方式。这样做的好处是,您可以继续添加模式,而无需更改代码,例如添加其他if条件等。

var allowedPatterns = [
    "MODULE=MESSAGE",
    "/message",
    "c=0",
    "SEQNO"
],
    href = location.href,
    valid = true;

for (var i=0, j = allowedPatterns.length; i<j; i++) {
    if ( href.indexOf(allowedPatterns[i]) === -1 ) {
        valid = false;
        break;
    }
};

if (valid) {
    //Your code
}