user1619891
user1619891

Reputation: 63

Changing HTML link based upon different parked domains

I have around 40 domains parked to one website.

I have a banner advert showing 'buy this domain' with a link to my domain portfolio.

I would like for this link to be dynamically changed, based upon which domain the user has input in order to access the domain. I would specify the link directing to that domain's individual sales page, as opposed to the overall portfolio.

Any insight would be a great help! Thanks

Upvotes: 2

Views: 406

Answers (3)

Markus Maga
Markus Maga

Reputation: 377

You can use window.location to get several different parts of the current website url.

For full url

window.location.href
"https://stackoverflow.com/questions/33484823/chaning-html-link-based-upon-different-parked-domains"

For domain only

window.location.hostname
"stackoverflow.com"

You could then use this window.location.hostname in the link you provide to your sales page (lets say www.sales.com), and pass it as a query parameter in the url.

// Navigate to sales page with domain as query parameter.
function goToSales() {
    location.href = "http://www.sales.com/?domain=" + window.location.hostname;
}

<a onclick="goToSales()">Buy domain!</a>

On the sales page you could then use javascript again and read the domain from window.location.search or any other prefered way to get query params in your server language.

For more examples of the attributes in window.location you can check this interactive site as referenced from here location.host vs location.hostname and cross-browser compatibility?

Upvotes: 2

toskv
toskv

Reputation: 31600

You can use location.host to find the domain of the current page, and use that info in javascript.

function goto(domain) {
  location.href = domain; // navigate to the given domain
}
<!-- location.host gives you the domain of the page -->
<h1 onclick="goto(location.host)">Buy this domain!</h1>

I made a running example here.

Upvotes: 1

Cruiser
Cruiser

Reputation: 1616

You'll want to examine window.location and parse the domain name from that, save it in a variable, then add it to your link.

Upvotes: 1

Related Questions