0

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

<script>var d=new Date();</script>
<li><a href="/controllername?t="+d.getTime()+"&name=.... // etc

How to add javascript datatime properly in the url?

5
  • can't you use php date() function..... date('Y-m-d H:i:s')..?? Commented Nov 12, 2012 at 10:43
  • I develop in Classic ASP, but it is necessary for cache to post the datetime with javascript Commented Nov 12, 2012 at 10:44
  • you are trying to use javascript outside the <script></script> tags Commented Nov 12, 2012 at 10:48
  • I know, but how to put the d.GetTime() value inside the html Commented Nov 12, 2012 at 10:50
  • You need to send most recent time value with url as shown in my example, problem with Bruno's code is that time gets appended to href value at page load that represents time when you loaded the page which can be different from the time you clicked the link. Commented Nov 12, 2012 at 11:22

7 Answers 7

6

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:

html

<ul>
    <li><a id="timeLink" href="#">​​​​​​​​​​​​​​​​​​Time Link</a></li>
</ul>​​​​​​

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:

$(document).ready(function(){
    var d = new Date();
    $("#timeLink").attr("href","/controllername?t="+d.getTime());
});​

Here's a functional fiddle

Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

<a href="#" onClick="this.href='/controllername?t=' + (new Date().getTime());">click me</a>

Comments

2

Use new Date().getTime(); and attach it to the link

<a id="link" href="/controllername?t="+d.getTime()+"&name=.... // etc

by grabbing the link element and appending the timestamp:

var link = ​document.getElementById('link');​​
var now = new Date().getTime();
link.setAttribute("href", "/controllername?t=" + now);

Since you want to add the current timestamp 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/.

1 Comment

this solution should be preferred, as it will attach the current datetime to the link at the moment of clicking, not the datetime of the event when the dom loaded.
0

Since your using ASP You can define your script as this. You can do any format you like (.ToString("hh:mm:ss") ) for example

<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

1 Comment

But how to pass that value to my url?
0

You cannot use the d object if you are not inside <Script>

do something like:

<script>
var d=new Date();
$("#mylink").attr("href","...." + d.getTime() + "..");

</script>

<a id="mylink">

Comments

0

Very easy just write this in your script

var d = '';

Comments

0

Time gets changed with every second, millisecond etc. so you need to send most recent time value with url:

<a href="javascript:sendRequest('/controllername?t=');">Send Request</a>

Javascript:

function sendRequest(oldUrl){
    var d = new Date();
    var newUrl = oldUrl + d.getTime();
    window.location = newUrl;
}

Here's is its DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.