Home > other >  How to replace part of the a link to the current URL by JavaScript or with jQuery?
How to replace part of the a link to the current URL by JavaScript or with jQuery?

Time:08-28

I've got a link that exists on many pages, which I would like to use JavaScript to make the later part of the link to be changed according to the current url* of the visiting page.

(*current url mentioned here = the current web address appears on the browser's address bar)

What is the right way to change URL after ?redirect= to the current visiting URL using JavaScript?

For example, this is the original:

<a href="https://app.example.com/?redirect=www.example.com">Link</a>

Now, the current url is https://www.example.com/game?q=123, the desired result would be:

<a href="https://app.example.com/?redirect=https://www.example.com/game?q=123">Link</a>

Thanks a lot

  • Related