Html 使用javascript动态链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18587633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dynamic link with javascript
提问by Ale
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.
在我的网页中,我想建立一个 HREF 链接,该链接在精确的网页中重定向取决于当前月份。
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 :
我在 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.
啤酒。
回答by Niels Keurentjes
HTML
HTML
<a href="#" id="myUniqueLinkId">name of link</a>
JS
JS
var month = (new Date()).getMonth();
var myURL = 'http://domain.tld/myLocalFile.php?month=' + month + '¶m=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.
最好的解决方案仍然不是在 JS 中而是在您的服务器端代码中处理这个问题,并在那里生成正确的链接。