add javascript datetime inside a html url - javascript

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?

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

Try this:
click me

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

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

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

Very easy just write this in your script
var d = '';

Time gets changed with every second, millisecond etc. so you need to send most recent time value with url:
Send Request
Javascript:
function sendRequest(oldUrl){
var d = new Date();
var newUrl = oldUrl + d.getTime();
window.location = newUrl;
}
Here's is its DEMO

Related

Data of saving file

In html file I need to display when that file was saved (time and date)
How can i do it? JavaScript?
<script>
var d = new Date(0);
document.getElementById("demo").innerHTML = d;
</script>
I need to know the the date when it has last been saved at the file system! And how can i change this 11-9-2014 at 23:7:55 to smth normal September 11 at 23:07?
document.lastModified will give you the time the document was last modified so far as the browser is able to determine it. You won't find anything closer unless you implement some method to embed the time stamp into the file when you save it.
Is this what you are looking for?
document.getElementById("demo").innerHTML = document.lastModified;
Demo here: http://jsfiddle.net/donal/w58v89Lh/

Need to include the parameter in javascript url in htmls

I have an requirement where I need to pass the parameter dynamically in script url.
Example:
"<" script src="myfile.js" "><"/script>"
now I need to pass the timestamp in the src like
"<"script src="myfile.js?tm=12345"><"/script">"
so whenever the this html load it should always take the new timestamp and pass in to the url.
"<"script src="myfile.js?tm=6788"><"/script">"
I would really appreciate in case some can help me to find the solution to this problem.
You can do something like this:
var elem = document.createElement('script');
elem.setAttribute('src',generateSrc()); // add your logic in generateSrc
document.body.appendChild(elem);
To create dinamically your tag with timestamp, you can do it in two different ways:
By using DOM:
var e = document.createElement('script');
e.setAttribute('src',"myfile.js?tm="+(new Date()).getTime()); //(new Date()).getTime() Returns the number of milliseconds since midnight Jan 1, 1970
document.getElementsByTagName("body")[0].appendChild(e);
By using innerHTML:
document.getElementsByTagName("body")[0].innerHTML+= '<script src="myfile.js?tm='+(new Date()).getTime()+'"></script>';
Here's http://cjihrig.com/blog/passing-arguments-to-external-javascript-files/ a tutorial that explain how to pass and retrive arguments to an external script.

JavaScript <div> .class Extraction

I am writing JavaScript that retrieves a webpage from another site, and only displays 'Notices' from a site.
Fortunately, all 'Notices' are div elements of class 'event'.
I want to extract only these divs from the returned code so that I can re-format and display them. All the code I have so far is working, but I'm not sure on how to extract the 'event' divs from the source code. Any ideas?
function getNotices(){
// Get the date from the form
var str = document.getElementById('formDate').value;
var year = str.slice(1,4); // Extract Year
var month = str.slice(6,7); // Extract Month
var day = str.slice(9,10); // Extract Day
// Inject correct date into URL
var link = "Raw Link";
// Write raw link to div for debugging
document.getElementById('rawLink').innerHTML = link; (debugging)
// Bounce off anyorigin.com to get the source
// Re-inject date into new link
var anyLink = "http://anyorigin.com/get?url=http%3A//ilearn.stpauls.school.nz/calendar/view.php%3Fview%3Dday%26course%3D%26cal_d%3D" + day + "%26cal_m%3D" + month + "%26cal_y%3D" + year + "&callback=?"; // Splice the date into the school link and site to bounce it off
$.getJSON('http://anyorigin.com/get?url=http%3A//ilearn.stpauls.school.nz/calendar/view.php%3Fview%3Dday%26course%3D1%26cal_d%3D30%26cal_m%3D7%26cal_y%3D2013&callback=?', function(data){
var obj = JSON.stringify(data); // Turn object into a string
document.getElementById('hopefullyTheData').innerHTML = obj; // Print string containing SPC website onto page (debugging)
});
}
Thanks guys!
Load the contents into jQuery then select by event class:
var events = $(data.contents).find('.event');
You can append only the event nodes by searching inside data.contents:
$('.event', data.contents).appendTo('#hopefullyTheData');
use $('div.event') selector to extract all the div element having class event.

How to get Client machine time on server on pageload event

I want to use client system date on page load event,
How can I do that.
Since this is not a post back so please let me know how to do it.
Thanks
var a = "&date=" + (new Date().replace(/\s+/g, ""));
Just append the value of a to all your links back to the server. On the serverside you will see the datetime come across in the HTTP request.
var currentDate = new Date()
The currentDate object will have the methods:
getMonth()
getDate()
getFullYear()
This should give you what you need.
Edit:
Since you mentioned time in the title of the post, it's probably worth mentioning that currentDate will also have:
getHours()
getMinutes()
should you want to display the current time as well.
Edit 2:
To tie into page loading
<script type="text/javascript">
function buildDate() {
var date = new Date();
/*build date string here*/
}
</script>
<body onload="buildDate()">
Other stuff here...
</body>

injecting today's date in javascript

When the page loads, I want a variable inside my JavaScript to hold today's date. So far I have this:
<script type="text/javascript">
var TodayDate = <%Eval('System.DateTime.Now') %>;
</script>
It massively bugs. Any suggestions?
Just use a javascript date object.
var today = new Date();
Or if you need the server time
var today = new Date('<%= DateTime.Now.ToString() %>');
is there anything in particular wrong with
<script type="text/javascript">
var TodayDate = new Date();
</script>
?
Why don't u use Javascript Date() object directly?
<script type="text/javascript">
var TodayDate = Date();
</script>
If you want to have a variable containing the date on the server, your way of doing is probably good (except for the missing apostrophes and the Date constructor). So it should be:
var today = new Date('<%= DateTime.Now.ToString() %>');
//this assumes that the server has such a locale that DateTime.ToString can be
//parsed by the javascript interpreter
But, if you need to have the date of the client:
var today = new Date();
You can use the previous people's answers of setting the date from within Javascript.
The problem you're seeing is that ASP is putting the string representing the current Date directly into the JavaScript source code. You need to put quotes around it so JavaScript sees a string and can parse it corretly.
Something like:
<script type="text/javascript">
var TodayDate = "<%Eval('System.DateTime.Now') %>";
</script>
which sets TodayDate to be a string. Are you going to use it as such or as a Date object?

Categories

Resources