I would like to create a link in HTML whose href
changes based on a GET parameter (example link)
As href
i try this
<a href="?url">text</a>
but it doesn't work
Here is a gif of how it should work
Sorry for my English
I would like to create a link in HTML whose href
changes based on a GET parameter (example link)
As href
i try this
<a href="?url">text</a>
but it doesn't work
Here is a gif of how it should work
Sorry for my English
I understand that you're trying to fill out the "href"-attribute of an anchorlink, by dynamically getting an URL from the GET parameter.
This can be done with the help of JavaScript. I found an example in another Stack Overflow question and made it fit your use case. In that same thread you'll find some good examples of other ways to do it as well.
Try out the following snippet:
<a id="myLink" href="">Text</a>
<script>
const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('link');
const myLink = document.getElementById('myLink');
if(myParam) {
myLink.href = myParam; /* The URL given as a parameter */
} else {
myLink.href = '#0'; /* If no parameter, the link leads nowhere */
}
</script>