The idea is to be sure it's an url with an http(s):// scheme or internal url.
function sanitizeUrl(url) {
try {
const u = new URL(url, document.baseURI);
if (
u.protocol === 'http:' ||
u.protocol === 'https:'
) {
return u.href;
}
} catch(e) {}
return '';
}
The idea is to be sure it's an url with an http(s):// scheme or internal url.