0

Would something like this be possible?

<head>
<script>
var movie="Tag";
var link=("google.com/"+movie);
</script>
<a href=(link)>Click Me</a>
</head>

Please Help Me

3
  • 1
    You don't. You can use an onclick though. Commented Sep 11, 2018 at 15:51
  • You -can- use anchor tags, you just need more javascript. stackoverflow.com/questions/14845710/… Commented Sep 11, 2018 at 15:52
  • Please use the search before you ask a new question ([javascript] url variable href). Most questions have been asked before. Commented Sep 11, 2018 at 16:01

2 Answers 2

2

HTML does not work like a templating language. To do something like this, the source would look a bit more like this:

<body>
  <a href="#" id="some-random-id">Click Me</a>

  <script>
    var movie = "Tag";
    var link = "http://google.com/"+movie;
    document.getElementById('some-random-id').setAttribute('href', link);
  </script>
</body>

The difference is that we first created the HTML, and afterwards are using javascript to replace the link.

It's also possible to use Javascript to write all the HTML, but this is probably the easiest way to get started.

Sign up to request clarification or add additional context in comments.

Comments

0

You can, but it takes a bit of coding.

In this case, you need to specify the full url, including the schema.

Then I gave the link an id attribute so that the JavaScipt code could find it and update the href with the new url.

const movie="Tag";
const url= "https://google.com/"+movie;
const link = document.getElementById("test-link");

link.href = url;
<a id="test-link" href="#">Click Me</a>

Comments