If the URL contains a string, then run the jQuery function?

On my site id I like to run the jQuery function if my url contains the word "test"

All the attempts I'm trying to do is if my url contains the string "rest", and then add a field to the element on the page?

Ive added jSfiddle to try and show what Ive done so far.

$(document).ready(function(){ // How can I check to see if my url contains the word 'TEST' then run the below function? $('.block-widget').css('margin-top, 252px') }); 
+4
source share
6 answers

Use window.location to get the current location url.

Depending on the condition, you can apply the margin top property.

 $(document).ready(function(){ var pathname = window.location.pathname; if(pathname.indexOf('text') > -1){ $('.block-widget').css('margin-top, 252px'); } }); 
+9
source
 $(document).ready(function(){ if (document.url.match(/test/g)){ $('.block-widget').css('margin-top, 252px') } }); 
+2
source

Take a look at this link that contains information about what you need to know about the 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') } }); 
0
source

Get path name:

var pathname = window.location.pathname;

Then check if it contains "TEST".

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

0
source

try it

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

name * = 'keyword' selector is a true option.

 <input name="man-news"> <input name="milkman"> <input name="letterman2"> <input name="newmilk"> <script> $( "input[name*='man']" ).val( "has man in it!" ); </script> 

Resource: https://api.jquery.com/attribute-contains-selector/

0
source

Source: https://habr.com/ru/post/1435541/


All Articles