0

I have total control over my entire domain and its subdomains, and I want to replace the URL displayed in the address bar with a subdomain but not redirect the page.

For example, when I load subdomain.mydomain.com?register I want the address bar to display register.mydomain.com without redirecting.

ALL pages and subdomains of mydomain.com already have document.domain = 'mydomain.com'; as the first thing in the $(window).load(function () { code of its external .js file.

I've tried history.pushState("", "", '/'); to strip away anything after the subdomain.mydomain.com (which works), and I've been able to display something after the / (which also works), but I can't get the displayed subdomain to change before the /.

How do I do this? Is it possible at all? If so, does it rely on document.domain? And if it does, am I executing document.domain properly??


Should I put

 <script type="text/javascript">
 document.domain = 'mydomain.com';
 </script>

by itself in the <head> of my HTML files, or will it work just fine being in the $(window).load(function () { code of its external .js file?

Jragyn
  • 29
  • 1
  • 7

1 Answers1

0

I'm not sure about domain/subdomain things, but I have a page where a user can click multiple items and depending on his/her input, a URL that is displayed in browser's address bar changes. There is how I do it without redirecting:

var newurl = window.location.protocol 
    + "//" + window.location.host + window.location.pathname 
    + StrippedQueryString + '&H=' + Parameter;

if (history.replaceState) {
    window.history.replaceState({ path: newurl }, '', newurl);
}

Hope this helps.

Sergii
  • 99
  • 3
  • 6
  • I ended up going with `pushState();` instead of `replaceState();` so that the back button would still work they way I needed it to. But thank you very much!! This would have done what I needed, but apparely not have allowed the back button to work properly. Source: [http://stackoverflow.com/a/17507140/3762525](http://stackoverflow.com/a/17507140/3762525) – Jragyn Jun 08 '16 at 05:15