Explain Codes LogoExplain Codes Logo

How to check if the URL contains a given string?

javascript
prompt-engineering
functions
callbacks
Alex KataevbyAlex Kataev·Sep 2, 2024
TLDR

Quick-and-dirty check for a certain string within a URL using JavaScript's includes() method:

let containsString = window.location.href.includes("franky"); // true means URL hits the 'Franky jackpot' console.log(containsString);

When a simple true or false will do

Specify targeted string, let's say "franky" for instance:

if (window.location.href.includes("franky")) { console.log("'Franky' spotted in the URL! Is he involved in any suspicious activity?"); // CSI: URL }

jQuery – for those with exquisite taste

With jQuery ready to save the day:

$(document).ready(function() { if(window.location.href.indexOf("franky") != -1) { // -1 basically means 'Franky is currently MIA in the URL' alert("'Franky' is gracing us with his presence in the URL"); } });

Ensure to include jQuery in your HTML for a fine dinner with jQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Regular Expressions - it isn’t as difficult as it sounds

You can treat "franky" as a movie star having his première with Regular Expressions:

if (/franky/.test(window.location.href)) { // Regular Expressions extend a Red Carpet to 'Franky' console.log("'Franky' is shining in the URL!"); }

When your browser wears vintage

Stick to indexOf() for ninth planet of the Solar system aka legacy browser-support:

let urlString = window.location.toString(); // Converting to String (or becoming fluent in URLish) let containsString = urlString.indexOf("franky") != -1; // when Franky decides to leave, he leaves -1 in his wake console.log(containsString);

Mystery of Query Strings with URLSearchParams

Unraveling Query Strings

Investigate if a specific "search" is hanging around the Query string:

let params = new URLSearchParams(window.location.search); let spiedParam = params.has("search"); // 'search' parameter behaving suspiciously in the URL console.log(spiedParam);

Snooping Parameter Values

Or snoop on value of a suspect named "search":

let value = params.get("search"); console.log(value); // Evident that 'search' haunts the URL in disguise

Overcoming Hurdles

Race against the Clock (dress rehearsal)

Ensure your code doesn't start checking the URL without eating its breakfast (load):

window.onload = function() { if(window.location.href.includes("franky")) { alert("'Franky', you can't hide in the URL anymore."); } };

Cap Sensitive includes()

The includes() method is quite a stickler for UPPER case and lower case. If you are easy-going, opt for toLowerCase():

let lowerCaseUrl = window.location.href.toLowerCase(); console.log(lowerCaseUrl.includes("franky".toLowerCase())); // true, now 'Franky' can't play the 'disguise-in-Uppercase' card anymore

Dealing with Tricky Hash values

If your strings like 'em hash tags (#):

console.log(window.location.hash.includes("#franky")); // true if "#franky" is a part of the tinsel party of the URL hash