Search code examples
javascriptjqueryhrefindexof

Using .indexOf on more than one url string


I've got a function that runs when a page's URL ends in /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');
            }
        });
    }
}

But I'd like to apply this function to other pages (contact.html, etc).

How can I make .indexOf work with more than one string?

Any help/advice/suggestions/constructive criticism is appreciated! I know enough to know that this is trivial, but I've been unsure as to what solution would work best here.


Solution

  • You can use .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>