Javascript Function Compare 2 URLS Same Ignore Trailing Slash /
Javascript Function to Compare if 2 URLS are the Same Ignoring Trailing Path Slash / Ignore Query String, Ignoring Hash
Home
Short:
function boolUrlsSame(baseUrl1, baseUrl2) { // create URL object to analyze url parts const url1 = new URL(baseUrl1); const url2 = new URL(baseUrl2); // remove ending / to prevent a false return false; ie because // pathnames same except ending '/' ie 'url.com' vs 'url.com/' path1 = url1.pathname.endsWith("/") && url1.pathname !== "/" ? url1.pathname.slice(0, -1) : url1.pathname; path2 = url2.pathname.endsWith("/") && url2.pathname !== "/" ? url2.pathname.slice(0, -1) : url2.pathname; // compare protocol, hostname, and pathname (ignoring query and fragment) return url1.protocol === url2.protocol && url1.hostname === url2.hostname && path1 === path2; }
source code home
S
H
A
R
E
Facebook
Twitter
LinkdIn
Reddit
Blogger
π Ώing
Email π§
π Multiple
Facebook
Twitter
Linkdin
Reddit
Blogger
Ping
Email
Share β