Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 377
Ale Avatar asked Aug 31 '25 22:08

Ale


1 Answers

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.

like image 123
Niels Keurentjes Avatar answered Sep 03 '25 10:09

Niels Keurentjes