在多个url字符串上使用.indexOf

时间:2017-06-26 06:29:26

标签: javascript jquery href indexof

我有一个在网页的网址以/about.html结尾时运行的功能

window.onload = function() {
    if (window.location.href.indexOf('/about.html') > -1) {
        $("#logo-black").typed({
            strings: ["Nothing^450&Co^250.^500", "^800__^400&Co^600."],
            typeSpeed: 70,
            backSpeed: 100,
            callback: function() {
                $(".typed-cursor").css("display", "none"),
                setTimeout(function(){
                    $('.main-load').toggleClass('main-load-active'),
                    $('.nav-text').toggleClass('nav-text-active');
                },400),
                $('.nav-reveal').toggleClass('nav-reveal-active');
            }
        });
    }
}

但我想将此功能应用于其他网页(contact.html等)。

如何让.indexOf使用多个字符串?

任何帮助/建议/建议/建设性批评表示赞赏! 我知道这很简单,但我不确定哪种解决方案最适合这里。

1 个答案:

答案 0 :(得分:2)

您可以使用.test()

console.log(window.location.href)
if (/(about.html|contact.html|js)/.test(window.location.href)) {
  console.log("Contains either: about.html OR contact.html OR js")
} else {
  console.log("Can't find either: about.html OR contact.html OR js")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>