[JavaScript] Rewrite a href URLs based on the currently accessed domain
I’ll introduce JavaScript sample code that rewrites a href URLs based on the currently accessed domain.
The currently accessed domain can be obtained with document.domain, so based on this, I’m rewriting href only when it’s a specific domain.
HTML
JavaScript
var button = document.getElementsByClassName('js-button');
if (button !== null) {
var href = button[0].getAttribute('href');
var domain = document.domain;
// Update URL only when currently accessed domain is test.example.com
if (domain === 'test.example.com') {
href = 'https://another.example.com/target_site';
}
}
That’s all from the Gemba that wants to rewrite URLs on the client side only for test sites.