Last Modified timestamp - javascript

I am trying to add the last modified timestamp on a webpage but I want the date to change only one the page is modified.
Here is the code I am using:
<script>
var date = document.lastModified;
document.write("Last Modified: "+date);
</script>
But this is showing the current date, so each time I visit the page I see today's date. How can I change to have the date updated only when the page is updated?
Many thanks.

That is because the js file gets sent to the client browser and the browser is executing that command to check on the lastModified - being current data/time of the client.
I do not see a way to get it from js - you would have to put that information from server side - will not be possible to read it on client side.

you can't use JS here because "lastModified" generates on runtime.
"lastModified" should be part of static page content (like Last Modified:
10/10/2020) or generated by BE and delivered to your page with ajax request.

You can't get this information by using only js. In this case you would need a server to keep track of the changes being made to a page and request this information with an http call.

Related

How to get local date & time of browser in the file when the file itself in rendered on the server

I am trying to the browser/local date and time in a file[PDF] . My issue is that the rendering of the file/PDF happens on the server itself. So there's no way I can think of in which I can use a JavaScript, since my rendering is happening on the server side.
Any suggestions on how can I pass my current browser time directly in that file or anyother suggestions which can help?
Template: <p>Generated {{today|date:'short'}}</p>
TypeScript: today: number = new Date();
You could pass your date from the client to the server via http and print that date on the pdf, then send the pdf to the client.

How to pass latest datetime of server to JavaScript function?

I have a text box in HTML and we have text box onkeyup event.
So I want the latest UTC date-time from the server(C#) once we press any key in the textbox. For that, I have added JavaScript function on "keyUp" event of that text box like below-
<type="text" onkeyup="return SetUTCDateTime('#DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss")'); />
<script type="text/javascript>
var currentUTCDateTime = null;
function SetUTCDateTime(currentMessageUTCDateTime)
{
currentUTCDateTime = currentMessageUTCDateTime;
}
</script>
I have taken the global variable in JavaScript to keep the time for further uses.
But my problem is when I enter any text into the textbox, the first time this function is giving correct time. But for any further/next clicks it's giving the same value but it should give the latest time.
FYI, I can't use client/browser time here because it may be invalid. That's why I want to use Server side DateTime value.
And also I don't want to call any API to get latest UTC time from the server(due to performance issues).
Is there any way to get latest server-side time each time we click's on the textbox ?
It looks like you're trying to render the server's datetime into the SetUTCDateTime method, but that's only happening once when the page renders.
Assuming there's a good reason for wanting to get the time from the server, what you'd need to do is code the SetUTCDateTime method to do an ajax request back to the server, and have that action on the server return the current datetime.
There is a problem with you perception of client-side and server-side and the way ASP.NET operates.
While you are trying to get the correct date/time from the server, you are never actually making a call to the server. The browser needs to contact the server to get the latest value of DateTime.UtcNow.
In your case, you are rendering the page with the value of DateTime.UtcNow as it was on the first HTTP request. Your HTML page is passed to the browser via HTTP, and the part below:
<type="text" onkeyup="return SetUTCDateTime('#DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss")'); />
is converted to the following (assuming the call was made at 13:00 of the 1st of January):
<type="text" onkeyup="return SetUTCDateTime('2017-01-01T13:00:00');" />
So after you receive the HTML from the server, the time isn't variable anymore, but fixed to the value it had after being translated by ASP.NET. The next time you are triggering an event you are actually simply calling the javascript below, as that is what your page contains:
return SetUTCDateTime('2017-01-01T13:00:00');
And so your event will give the same value.
To do what you are trying to achieve, you have two options:
Create a service that yields the latest date/time and call the
service via AJAX on the event you are implementing, or
(Not the best
way to achieve this) post the page each time your event fires and it
will automatically give the latest date with the code you already
have.
But you need to make an HTTP request to the server if you need the latest date/time from the server.

How to execute a page's javascript function in perl?

I am trying to extract data from a website using Perl. Below is the description of the site:
site displays data dependent on a date
a calendar is displayed that is used to change the date
upon clicking the dates in the calendar, it calls a javascript function that passes in the date and refreshes the part of the page that displays the data
My question is, how do I execute that JS function so that I could loop through the dates that I need data from?
Thanks in Advance
It's much easy make same HTTP request from your script and get all data you need directly.
You can record all HTTP requests/responses of your browser by using HTTP Fox extension (for Firefox).
Edit
There is a CPAN module:
JavaScript - Perl extension for executing embedded JavaScript
But I've not already tested them.
Original post:
Take a look at smjs it's Spidermonkey's JS shell.
You could pass javascript by:
open my $jsout,"echo '$javascript' | smjs |" or die;
print <$jsout>;
...
But be care! This take security consideration away!

I want one calendar for datepicker textfield. In which I want that my calendar should show current date as my server current date. how to do it...?

Right now I'm using one js file containing some function of that calendar code but my calendar always showing current date as client date. I tried a lot but finally I knew that calendar of javascript always take the client date and time .So can you please tell me how my web application calendar will show me the current date as server current date instead old client current date
As you know that js always take client side date so you have to make trick for it
one trick for which i go is
on the time of rendering html i will take server side date into an hidden field and when jquery calll the current date i will pass it as a current date
You could send a value from the server which holds the current server time and create a new date object on the client side which holds that value, hence effectively showing the date on the server.
However, there has to be a VERY good reason for doing so, as this can be very confusing to the user.
Also, your server may be located in one timezone right now, and another tomorrow (if you move to another provider), so the dates may be inconsistent if you save a response from the user.
If you grow a bit bigger and need redundancy across data center, you will definitely have different time zones as you will want your data centers to be distributed geographically.
So, what's usually a best practice is to show the user his own time, and translate it to whatever you want on the server.
you may simply read it a variable during page load like:
$startOfMonth= date("01-m-Y");
and assign it yo your datepicker field like
">
Please Try this
$( ".selector" ).datepicker({ defaultDate: "<?php date("m-d-y"); ?>" });

how to get LOCAL TIME, NOT server time?

I use these lines of code to find out the local time in Gujarat - India, regardless server time set correct or not. But i noticed that this is not working. This line produce SERVER TIME, but not LOCAL TIME. It cause problem if server time change accidentally.
What is the exact solution to to get LOCAL TIME in any country in the world?
Dim zoneId As String = "Indian Standard Time"
Dim tzi As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(zoneId)
Dim result As DateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi)
You may want to take a look at Noda, a .net port of Joda an open source project for working with dates. This has been ported by Stack Overflows, Jon Skeet.
A couple Link that might be helpful.
http://www.codeproject.com/Articles/78213/Noda-DateTime-Extensions-for-NET
enter link description here
What you want is the correct local time, regardless of the server defined time.
First of all, the server should be configured with the correct date/time. If you can't trust it, I think you have to rely on a service provided by a third party provider to tell you the correct time.
This other question suggests services you can connect to: Web service - current time zone for a city?
If what you want is the client's time, you have to get it through Javascript and send it to the server side.
Good luck!

Categories

Resources