views:

940

answers:

3

Hi Folks,

I am using jQuery currently and I am looking for a way to hide if the url contains /blah/.

Thanks in advance for your help. I am a js Noob and know it can be accomplished with regex somehow but don't have the time to learn this right now. I have a deadline I need to meet!

Thanks, Jack

+1  A: 

Sure, here you go:

Hide 'my_other_id' if the url contains 'foo'

if ($('a[href*=foo']).size() > 0) $('#my_other_id').hide();

If you want to do this when the page loads, use this:

$(document).ready( function() {
  if ($('a[href*=foo']).size() > 0) {
    $('#my_other_id').hide(); 
  }
});
btelles
missing the `]`?
Michael Haren
Thanks Michael. Corey, I rewrote the answer based a little more on what it seems you want.
btelles
+3  A: 

I think he wants to hide an element if the URL contains that part.

if (/\/blah\//.test(window.location)) {
    $('#element').hide();
}
Corey
Hi There. You are on the money. only if the url contains /blah/ would I hide the element. In your example, what does .test represent?Thanks again.
Jackson
Your code works PERFECTLY BTW! Thank you so much!
Jackson
Cool, no problem. `/\/blah\//` is a Regex object that searches for any occurrence of '/blah/'. `test()` is a Regex function that tests to see if the argument has a match to that regex. So if `window.location` contains '/blah/', then execute the following code.
Corey
A: 

Hi Everyone and thank you for your responses. For some reason it did not save my whole message..... Weird!

The full version went more like this:

Hi Folks,

I am using jQuery currently and I am looking for a way to hide an element if the url contains /blah/.

if the url contains /foo/ addClass to <div id="blah"></div> so the class is added like <div id="blah newclass"></div> then I can write css for .newclass {display:none;}

I hope this makes more sense. I'm going to hop in and try your suggestions now. THANK YOU!

Jackson