javascript - 与javascript的动态链接

标签 javascript html

在我搜索这个主题的过程中,我想得到一些帮助。

在我的网页中,我想建立一个 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***

拜托,你能给我一些建议吗?

麦芽酒。

最佳答案

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 中而是在服务器端代码中处理此问题,并在那里生成正确的链接。

关于javascript - 与javascript的动态链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18587633/

相关文章:

javascript - 在选择元素中选择选项元素时更改浏览器的蓝色默认颜色

javascript - 从 DOM 中移除的元素取消绑定(bind)事件

javascript - BackBone解析json集合

html - 继续扩展内容而不是将页脚向下推

javascript - SVG路径半圆中的圆 Angular

html - Angular 2 - 变量更改时更新 View

javascript - Webpack 包 4.41.2 安全问题,因为 terser-webpack-plugin 使用了 serialize-javascript 版本 1.9.1

javascript - 按浏览器窗口宽度重定向到另一个页面 JavaScript

javascript - Canvas 数据到网络 worker

javascript - 在 powershell 中使用 JScript