Search code examples
javascriptjquerybusiness-catalyst

jQuery VAR in IF statement usage


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();
}

Solution

  • 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();
    }