congaaaron
congaaaron

Reputation: 13

change and display hostname of url

I have two sites: one for an internal audience and one for an external audience. The site URLs will match other than the hostname. What I would like to do is have a button available on the internal site so that someone can click and be given the external URL of that page.

Essentially I want to replace the hostname of the current URL using javascript or jquery - whatever is quicker and easier.

Upvotes: 1

Views: 995

Answers (1)

Paul
Paul

Reputation: 141917

If you want to redirect the current tab you can just set the hostname directly:

$( '#external-link' ).click( function ( ) {
    location.hostname = 'external-domain.com';
} );

If you want it to open in a new tab you can do something like this:

$( '#external-link' ).click( function ( ) {
    this.href = '//external-domain.com' + location.pathname + location.search + location.hash;
} );
<a href="#" id="external-link" target="_blank">Open External Page</a>

Upvotes: 4

Related Questions