Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

javascript - Change button HREF depending on the address bar URL

I have a website that users with an affiliate URL often visit, for example:

https://demowebsite.com/?ref=550

There is a link that initially has an URL:

https://demowebsite.com/register

<a href="https://demowebsite.com/register" id="reg" class="btn btn-info">Register</a>

How can I make that if there is a part with a ref ID in the address bar like https://demowebsite.com/?ref=550, then the href is transformed into the following by click:

https://subdomain.demowebsite.com/ref/

and add the ref ID at the end as an append:

https://subdomain.demowebsite.com/ref/550

And if there is no affiliate part in the address bar, then the link would remain the same.

<a href="https://demowebsite.com/register" id="reg" class="btn btn-info">Register</a>

I would be grateful for any help.

question from:https://stackoverflow.com/questions/65918813/change-button-href-depending-on-the-address-bar-url

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the location object given in tabs

<script>
var href=location.href
if(href.includes('ref')){
  document.getElementById('reg')
  .href="https://demowebsite.com/ref/"  +  href.split('=')[1]
}
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...