JavaScript test if url does NOT contain some text


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.





I strongly recommend developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… it´s very good documented.
– Ziinloader
Jun 17 '14 at 21:47




2 Answers
2


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





Heh, I think so too. It works.. I got my own version to work too after I created it but yours is much shorter: if (document.location.href.indexOf('RecordID') > -1){ alert('This url contains "text"'); } else { alert('This url DOES NOT contains "text"'); }
– Yevgen
Jun 17 '14 at 21:46



if(document.URL.indexOf("YourText") <= -1){
"Check"
}





Is there any situation where .indexOf() would return a number smaller than -1? (rhetorical question)
– JJJ
Apr 19 '16 at 8:48


.indexOf()





The OP got an answer 1 year ago, and accepted it (that means it worked). Why post another so late, just duplicating the other answer, with no explanation to how it works whatsoever?
– Druzion
Apr 19 '16 at 11:00






Techniques change over the years and different problems may have appeared. It is a valid question.
– Lisa Cerilli
Dec 24 '17 at 10:36






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift