Skip to content

Instantly share code, notes, and snippets.

@giladaya
Created August 18, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giladaya/dd4e761ce20a4da802c8 to your computer and use it in GitHub Desktop.
Save giladaya/dd4e761ce20a4da802c8 to your computer and use it in GitHub Desktop.
if we are on a subdomain, change links on page that point to the main domain to point to the same path in the subdomain
(function subdomain() {
var hostParts = location.host.split('.');
if (hostParts.length < 3) {
return;
}
var subdomain = hostParts[0];
var domain = hostParts.slice(-2).join('.');
var a = document.getElementsByTagName('a'); // grab every link on the page
var prefix = 'http://'+domain;
for (var i = 0; i < a.length; i++) {
var href = a[i].href;
if(href.indexOf(prefix) == 0) {
a[i].href = href.replace(domain, location.host);
console.log(a[i].href);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment