Concepts

Navigation destination allowlist

Try the free JavaScript compiler

Parse against a trusted base and require exact scheme, origin and known pathname; reject credentials.

Formal Syntax & Structure

javascript syntaxjavascript
new URL(input, fixedBase)

Working Example

Example Snippetjavascript
(async () => {function safe(s){try{const u=new URL(s,"https://shop.example/");return u.origin==="https://shop.example"&&u.protocol==="https:"&&!u.username&&!u.password&&["/catalog","/account"].includes(u.pathname)?u.href:null;}catch{return null;}}console.log(safe("/catalog"),safe("https://other.example/catalog"));})()
JavaScript Reference