简体   繁体   中英

jQuery—Checking URL for String

I'm trying to set up a jQuery script to control the appearance the advanced section of a search form. If a search hasn't been submitted, the div containing the advanced search should remain hidden. But if a search has been submitted as designated by the URL containing the string "Search", show the advanced search div .

The problem I'm seeing is that no matter what's in the URL it's always showing the #advsearch div through a fade in. It never returns false. What am I doing wrong? Thanks for your help!

if(window.location.href.indexOf("Search")) {
$('#advsearch').fadeIn()
}
else {
    $('#adv').click(function() {
    $('#advsearch').slideDown()
    });
}

If the string cannot be found, indexOf returns -1 , which evaluates to true .

Do:

if(window.location.href.indexOf("Search") > -1)

Depending on where the string is in the URL, you might just want to search in the pathName , search or hash part of the URL. Have a look at the documentation of the location object .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM