4

During my searching on this topic, I would like to have some help.

In my web page, I would like to build a HREF link that redirects in a precise web page depends to the current month.

My link is :

<td><a href="/comptes/mon_compte.html?display=affilies_periode&id=${vendeur.id}&  month=javascript:monthNumber">Détails pour le mois en cours</a></td>

And my code in JS :

<script language="JavaScript">
    date1 = new Date();
    document.write("<p>date1</p>");
    monthNumber = date1.getMonth();
    document.write("<p>monthNumber</p>");
</script>

with the result of month, I would like to make the query dynamic like this :

http://localhost:8080/comptes/mon_compte.html?display=affilies_periode&id=2&***month=javascript:monthNumber***

Please, could you give me a piece of advice?

Ale.

1
  • You want to make a dynamic link? Commented Sep 3, 2013 at 8:42

1 Answer 1

12

HTML

<a href="#" id="myUniqueLinkId">name of link</a>

JS

var month = (new Date()).getMonth();
var myURL = 'http://domain.tld/myLocalFile.php?month=' + month + '&param=1';
document.getElementById('myUniqueLinkId').href = myURL;

Or alternatively just handle it completely at runtime:

<a onclick="window.location='http://domain.tld/myLocalFile.php?month=' 
   + (new Date()).getMonth();return false;" href="#">name of link</a>

Best solution is still to handle this not in JS but in your serverside code and just generate the correct links there.

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

2 Comments

Thanks for the tip. Just a question about #, what's the meaningful of <a href="#" here?
An anchor is required to have a filled href attribute by HTML standards, to validate successfully. Since # only points to the top of the current page and can never cause navigation href="#" is the generally accepted "this link will never work regularly" notation. In the first example it's overridden by JS, in the second it's bypassed in favor of the onclick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.