jquery how to check if url contains word?
Try:
if (window.location.href.indexOf("catalogue") > -1) { // etc
indexOf doesn't return true/false, it returns the location of the search string in the string; or -1 if not found.
Seeing as the OP was already looking for a boolean result, an alternative solution could be:
if (~window.location.href.indexOf("catalogue")) {
// do something
}
The tilde (~
) is a bitwise NOT operator and does the following:
~n == -(n+1)
In simple terms, the above formula converts -1 to 0, making it falsy, and anything else becomes a non-zero value making it truthy. So, you can treat the results of indexOf
as boolean.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#(Bitwise_NOT)