-2

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.

Yogu
  • 9,165
  • 5
  • 37
  • 58
  • If you still need it, you may use something like this: `javascript:location.href=location.protocol+%22//%22+%22www%22+%22.%22+(location.host.split(%22.%22).length==3?location.host.split(%22.%22).slice(1,location.host.split(%22.%22).length).join(%22.%22):location.host)+location.pathname+location.search;` (found the orginal solution [here](http://www.evanconkle.com/2012/03/switch-domain-bookmarklet/)) – A S May 09 '15 at 04:34

1 Answers1

2

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.

showdev
  • 28,454
  • 37
  • 55
  • 73