Dynamic linking

I have a link in “site_a” to open a specific page in “site_b”.
Example: “http://www.site_b.com/results2010.html”

But, every year, I have to change the link, in order to open the page concerning the current year.
Example: “http://www.site_b.com/results2011.html”…“http://www.site_b.com/results2012.html”…
and so on.

My question is:
Is there a way to write the link in order to be permanent?
Example: “http://www.site_b.com/resultsthis year.html”

Thank you in advance.

Yeah there you have it.

You can use something like this inside your a tag. For ex.

...com/results${new Date().getYear()}.html

The point is to to embed javascript inside template strings.

I’m afraid that won’t work. HTML won’t parse JS.

Thank you Phillip, I will try it as soon as possible.
It didn’t work! Thanks any way!

Wow, I’m sorry to ear that Jenovs, but thanks for your attention.

How about you just add a bit of JS to make the link? Considering how little it is you can probably get away with using it on the page inside script tags.

Not sure if you have to support old browsers or if using template literals is OK, but here is the version without.

<footer>
  <a href="#" id="link">Like to some other site</a>
</footer>

<script>
  const year = new Date().getFullYear();
  const link = document.querySelector('#link');
  const url = "https://somewebsite.org/results" + year + ".html";
  link.setAttribute('href', url);
  link.setAttribute('target', '_blank');
</script>
2 Likes

How about? Ah, ah! How about a big hug? Thank you very much Lasjorg for your contribution, it worked fine. Now I just have to study that script to learn some more JS. Thanks.