Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript test if url does NOT contain some text

I found many option of checking if the URL contains some text but I am trying to do the opposite.

How can I start a function if the URL DOES NOT contain 'text'?

Here is what I started with that does fire the function but only if the url contains it:

if (document.location.href.indexOf('text') > -1){ 
alert('URL should not have "text" in it');
    }

I tried adding a '!' and a 'NOT' in front of the '(document...' but I guess that's not it.

like image 949
Yevgen Avatar asked Nov 28 '22 15:11

Yevgen


1 Answers

if (document.location.href.indexOf('text') === -1){ 
    alert('URL should not have "text" in it');
}
like image 84
BastienSander Avatar answered Dec 04 '22 23:12

BastienSander