How to execute a function if url contains "x" in jQuery

I would like to execute the up () function; if url contains "invt".

So far I have this:

if(window.location.pathname == "/invt/"){ alert("invt"); } 

However, this was not a warning to me. Any ideas?

Since then I have succeeded, thanks to the answers below.

Consider:

 $(document).ready(function () { if(window.location.href.indexOf("invt") > -1) { //checks url for "invt" (all products contain this) up(); return; } }); 
+4
source share
2 answers

Try the following:

 if ( window.location.pathname.indexOf('invt') >= 0 ) { alert("invt"); } 
+3
source

You can use indexOf() :

 if (-1 != location.pathname.indexOf('invt')) { alert ("GO"); } 

Demo

Try before you buy

+3
source

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


All Articles