3

このトピックを検索している間、私は助けを求めています。

私の Web ページでは、現在の月に応じて正確な Web ページにリダイレクトする HREF リンクを作成したいと考えています。

私のリンクは:

<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>

そして JS の私のコード:

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

月の結果を使用して、クエリを次のように動的にしたいと思います。

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

アドバイスをいただけますか?

エール。

4

1 に答える 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;

または、実行時に完全に処理するだけです。

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

最良の解決策は、これを JS ではなくサーバー側のコードで処理し、そこに正しいリンクを生成することです。

于 2013-09-03T08:42:06.977 に答える