在 html url 中添加 javascript 日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13342243/
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
add javascript datetime inside a html url
提问by user1469734
For a cache issue, I want to send the current datetime with a url, but with the following code, the current timestamp isn't shown in the url..
对于缓存问题,我想使用 url 发送当前日期时间,但是使用以下代码,当前时间戳未显示在 url 中。
<script>var d=new Date();</script>
<li><a href="/controllername?t="+d.getTime()+"&name=.... // etc
How to add javascript datatime properly in the url?
如何在 url 中正确添加 javascript 数据时间?
回答by Bruno Vieira
Instead of using inline javascript a better approach would be to identify your element in the DOM and then simply set it's href
attribute like the following:
与使用内联 javascript 不同,更好的方法是在 DOM 中识别您的元素,然后简单地设置它的href
属性,如下所示:
html
html
<ul>
<li><a id="timeLink" href="#">??????????????????Time Link</a></li>
</ul>??????
javascript
javascript
var d=new Date();
var timeLink = document.getElementById("timeLink");
timeLink.setAttribute("href","/controllername?t="+d.getTime());
As usual, using jQuery
can simplify a lot:
像往常一样,使用jQuery
可以简化很多:
$(document).ready(function(){
var d = new Date();
$("#timeLink").attr("href","/controllername?t="+d.getTime());
});?
Here's a functional fiddle
这是一个功能性的小提琴
回答by Akos K
Use new Date().getTime();
and attach it to the link
使用new Date().getTime();
并将其附加到链接
<a id="link" href="/controllername?t="+d.getTime()+"&name=.... // etc
by grabbing the link
element and appending the timestamp:
通过抓取link
元素并附加时间戳:
var link = ?document.getElementById('link');??
var now = new Date().getTime();
link.setAttribute("href", "/controllername?t=" + now);
Since you want to add the currenttimestamp when you click on a link, I would attach the above code to the onclick
function of the href
, so when you click on the link the actual timestamp will be appended to the url. See the example http://jsfiddle.net/GBBdc/.
由于您想在单击链接时添加当前时间戳,因此我会将上述代码附加到 的onclick
函数中href
,因此当您单击链接时,实际时间戳将附加到 url 中。请参阅示例http://jsfiddle.net/GBBdc/。
回答by kolufild
Try this:
试试这个:
<a href="#" onClick="this.href='/controllername?t=' + (new Date().getTime());">click me</a>
回答by Coder
Time gets changed with every second, millisecond etc. so you need to send most recent time value with url:
时间每秒、毫秒等都会发生变化,因此您需要使用 url 发送最近的时间值:
<a href="javascript:sendRequest('/controllername?t=');">Send Request</a>
Javascript:
Javascript:
function sendRequest(oldUrl){
var d = new Date();
var newUrl = oldUrl + d.getTime();
window.location = newUrl;
}
Here's is its DEMO
这是它的DEMO
回答by N Khan
Very easy just write this in your script
非常简单,只需在您的脚本中编写此内容即可
var d = '';回答by Riaan Walters
Since your using ASP You can define your script as this. You can do any format you like (.ToString("hh:mm:ss") ) for example
由于您使用 ASP 您可以这样定义您的脚本。你可以做任何你喜欢的格式 (.ToString("hh:mm:ss") ) 例如
<script> var d = '<%=DateTime.Now.Ticks %>';</script>
This way you can have any format and use it backend any way you like, always ensuring you have the correct format, as apposed to client side strings which may be in a localized format
通过这种方式,您可以拥有任何格式并以任何您喜欢的方式在后端使用它,始终确保您拥有正确的格式,就像可能采用本地化格式的客户端字符串一样
回答by YardenST
You cannot use the d object if you are not inside <Script>
如果你不在里面,你就不能使用 d 对象 <Script>
do something like:
做类似的事情:
<script>
var d=new Date();
$("#mylink").attr("href","...." + d.getTime() + "..");
</script>
<a id="mylink">