How do I get this bookmarklet to take me from abc.example.com
to example.com
?
I tried the following:
javascript:location.pathname%20=%20"";%20void%200
But it takes me to the same sub-domain (abc.example.com
) and not example.com
.
How do I get this bookmarklet to take me from abc.example.com
to example.com
?
I tried the following:
javascript:location.pathname%20=%20"";%20void%200
But it takes me to the same sub-domain (abc.example.com
) and not example.com
.
location.pathname
returns only the parts of the URL after the domain.
You might have more luck with location.hostname
, which returns the domain of the URL.
Here's a javascript example that returns the last two parts of the hostname (split by "."):
var loc = window.location;
var parts = loc.hostname.split('.').reverse();
if (parts.length > 2) {
parts.length = 2;
}
var newloc = parts.reverse().join('.');
console.log(loc + " > " + newloc);
This outputs example.com
from subdomain.example.com
.
Try it here (jsfiddle).
In that example, I'm hardcoding the hostname rather than using window.location.hostname
.