Ale
Ale

Reputation: 155

dynamic link with javascript

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.

Upvotes: 4

Views: 40876

Answers (1)

Niels Keurentjes
Niels Keurentjes

Reputation: 41968

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.

Upvotes: 12

Related Questions