1

I am sure that this will be a simple solution for someone well versed in jquery.

I am wanting to pass the path name into the if statement so

http://address.com/catalog/product <= catalog then gets passed into the if statment.

if (/\/catalog\//.test(window.location)) {
  jQuery('#name-div').hide();
}

so it hides a div if its a child of http://address.com/catalog

var url = location.pathname.split("/")[1];

if (/\/url\//.test(window.location)) {
  jQuery('#name-div').hide();
}
stealthyninja
  • 10,343
  • 11
  • 51
  • 59
user1095118
  • 4,338
  • 3
  • 26
  • 22

3 Answers3

0

you can do it in many way like:

if( window.location.indexOf(url) !== -1 )

or

if( window.location.search( /url/ig ) )

or

if( window.location.match( /url/ig ).length > 0 )

In this examples you don't even need to use jQuery. It is just normal javascript.

dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
0

You basically have the answer to your own problem.

$(function(){
    var url = window.location.href;

    if( /catalog/i.test(url) )
        $('#name-div').hide();
});

Unless you have other URLS with catalog in them, there's no reason to dissect the URL any further. Just make sure you select your element after the DOM is ready as I did in my example.

THEtheChad
  • 2,372
  • 1
  • 16
  • 20
0

As I read your question, what you need is to create a RegExp object from a string since your want to pad with / characters:

var url = location.pathname.split("/")[1],
    re = new RegExp('/' + url + '/'); // Create RegExp object from padded string

if (re.test(window.location)) {
    jQuery('#name-div').hide();
}
jensgram
  • 31,109
  • 6
  • 81
  • 98