Get relative URL from absolute URL

A nice way to do this is to use the browser’s native link-parsing capabilities, using an a element:

function getUrlParts(url) {
    var a = document.createElement('a');
    a.href = url;

    return {
        href: a.href,
        host: a.host,
        hostname: a.hostname,
        port: a.port,
        pathname: a.pathname,
        protocol: a.protocol,
        hash: a.hash,
        search: a.search
    };
}

You can then access the pathname with getUrlParts(yourUrl).pathname.

The properties are the same as for the location object.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)