Skip to content Skip to sidebar Skip to footer

If URL Contains String Then Run JQuery Function?

On my website id like to run a jQuery function if my url contains the word 'test' All im trying to do is if my url contains the 'rest' string then add a margin to an element on the

Solution 1:

Use window.location to get the current location url..

Based on the condition you can then apply the margin top property.

$(document).ready(function(){         
     var pathname = window.location.pathname;
     if(pathname.indexOf('text') > -1){
        $('.block-widget').css('margin-top, 252px');
     }     
});

Solution 2:

$(document).ready(function(){         
   if (document.url.match(/test/g)){
     $('.block-widget').css('margin-top, 252px')     
  }
});

Solution 3:

Take a look at this link that contains details that you need to know about URL.

https://developer.mozilla.org/en-US/docs/DOM/window.location

$(document).ready(function(){         
    if (window.location.href.indexOf('rest') >= 0) {
      $('.block-widget').css('margin-top, 252px')     
    }
});

Solution 4:

Get the path name:

var pathname = window.location.pathname;

Then check if it contains "TEST".

if (pathname.toLowerCase().indexOf("test") >= 0)


Solution 5:

try this

$(document).ready(function(){         
    var patt=/test/g;
    if(patt.test(window.location.pathname)){
    $('.block-widget').css('margin-top, 252px')  
  }   

});

Post a Comment for "If URL Contains String Then Run JQuery Function?"