Reputation: 2453
the url is as this: http://example.com/download/
var pathname = window.location.pathname;
if(pathname=='download/'){
$("#subnav-content div:first").hide();
$("#subnav-content div:second").show();
}
why the above code in jquery doesn't work? i want to when the url is http://example.com/download/. show the secong div.
ps*:does this check affect the site performance?*
Upvotes: 1
Views: 129
Reputation: 4085
You need the leading slash.
'/download/'
If you expect query string parameters you may try a regular expression to just match the download portion of the url: the following matches /download/.
if (window.location.pathname.match(/^\/download\//i))
Regarding the jquery, there is no :second, you need to use :eq(1)
var pathname = window.location.pathname;
if(pathname=='/download/'){
$("#subnav-content div:first").hide();
$("#subnav-content div:eq(1)").show();
}
Response to comments
I'm putting my comment here because the formatting is horrible in the comments. The regular expression for matching download can be summed up as follows:
/
- start of regular expression matching syntax
^
- means start matching at the very start of the screen
\/
- means match the literal string '/', which is a special character which must be escaped
download
- match the literal string 'download'
\/
- again means match the literal string '/'
/
- end of the matching syntax
i
- regular expression options, i means ignore case
It was not clear to me what your other note was asking for.
Upvotes: 2
Reputation: 5075
Try using
$("#subnav-content div:eq(0)")
$("#subnav-content div:eq(1)")
Also, you need to bind the code to an Event that will get fired when the Document is ready(load
, or onDOMReady
where supported) otherwise the div
s might not exist in memory yet.
ps*:does this check affect the site performance?*
Every line of code has an effect on site performance. Not necessarily a visible one although.
Upvotes: 1
Reputation: 7991
Second is not a selector. You want:
$("#subnav-content div:nth-child(2)").show();
Upvotes: 2