Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 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 Reply

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...