开发者

jQuery—Checking URL for String

开发者 https://www.devze.com 2023-03-04 16:32 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消