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.
Related
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.
I am trying to automate a task of authentication via Azure ActiveDirectory. I was writing my script as per the browser requests. On seeing the request, I notice that browser is sending a parameter in url-encoded form.
The parameter is being sent as i19.
<input type=hidden name=i19 data-bind=\"value: timeOnPage\"/>'
On going through internal scripts of the page I find the above input type where i19 is being assigned a value dynamically.
I need to generate this value to form a encoded string parameter to be sent to next location url for authentication.
It does not look like an EPOCH UNIX timestamp either.
For Eg: At roughly about 13:53:58 IST, i19: 55964
Now I am unable to find what exactly is going in this i19.
I am attaching a response script here for the script where this input is being generated.(It is a long file so attached separately). All I know that some value is being created dynamically and browser understands it but via python post request, javascript is disabled and the value is not being shown in response/headers/cookies.
I shall be really grateful for the same.
Im trying to get the date from a date picker i have on my page and then have that date posted to a php function which will store the selected date in my database.
I think you have a misunderstanding of how Javascript, HTML forms, and PHP work together.
Javascript (and jQuery) run in the browser. PHP runs on your server.
The browser initiates a request to your server, your server hands off the request to a php process which parses and runs your php. This generates html which is sent back to your browser. The javascript linked to or contained in <script> tags is then loaded, and run if necessary (or triggered by events). This is an oversimplification, but should get across the basics.
So, what is happening is the jQuery is running inside your browser to perform changes to the HTML. Specifically, it is inserting a string formatted as 'MM-DD-YYY' into your form field. When the user then posts this form, those values get added to the HTTP request that is sent to your server. Those values are then accessible in the $_POST variable in your php script, as a key=>value pair, where key is the HTML name='formfield1' attribute of your form. So your would access that as $date = $_POST['formfield1'].
Javascript, and jQuery specifically are doing nothing but loading a string value into that html field for the user. You can't access Javascript variables from your PHP.
If you post your form code, a more specific answer might be able to be given.
Edit: for an inline datepicker, you will need to configure your datepicker as follows:
$('#datepicker').datepicker({
inline : true,
altField : '#hiddenFieldID',
});
where #hiddenFieldID is a hidden input field in your form. Regardless, you have to attach the date value to your form in some way for it to be available in $_POST.
http://www.tumangobajito.com/auctions/Blackberry-9360-Blanco-484
I have used WebBrowser control to navigate this link, but there is javascript error, so I cannot see the timer working.
Is there anyway for me to get this timer to a textbox in C# form ?
The timer is being compared against an internal end-time field. You could attempt to reverse engineer the scripts and see if that data is something you have access to (there is 8 Javascript files active so I'll let you take a look).
Another method would be to use C# to download the page. Then parse the data to look only for what is needed.
<div id="timer_484" class="timer countdown" title="1341363012">
CAPTURE THE DATA HERE
</div>
If you're needing the times from different pages, you'll have to modify your parser to fit accordingly. Once you have that data though, you could write your own countdown timer (if that's the route you're taking with this).
I am building a survey page where users have a limited time to answer all their questions. The time given is stored in the model as #test.time_allowed, which is an integer representing seconds.
I need to have a simple and non-user-tamperable way to get a timer to display on the view and execute a controller action when it winds down to 0. How can I accomplish this?
I'm a relative beginner so any specific answers would be really helpful. Thank you.
---UPDATE---
#Bryan:
I assume there is a tamper proof way if the timing is done server side? For example, there might be a javascript timer on the client side as you suggested, but upon submission can't the submission time be checked against the time of the window's initial load?
Since data coming back from the client can never be fully trusted, the server must somehow know what the timestamp of the originally generated form was. This could be done by saving variables in the session or database, but this is problematic. Instead, the server can place a timestamp in the form, either encrypted, or signed, to ensure the client has not altered it. The server can then reject the submission as necessary. On the client, separate logic can handle the UI portion, giving the user feedback on the time limit, but ultimately this only loosely coupled to the server handling.
Details:
The server should generate two form fields: one with the system timestamp time = Time.now.to_i to track when the form was generated, and another with a signature Digest::MD5.hexdigest(time.to_s.concat('Some-secret-string-1234')) (note using the same time value here for the timestamp form field and signature form field). This validates that the form is submitted with a server-generated timestamp that has not been altered by the client.
You might also send another form field with the time limit.
On the client, read the timestamp, use setTimeout and the time limit to generate a countdown or whatever you want to do on the front end.
When the form is submitted, authenticate the timestamp submitted with the form by regenerating the MD5 signature using the same method as before. Make sure it matches the signature submitted by the form. Then, add the timestamp to the timeout, and make sure it's later than the current server time. If so, you have a valid submission, within your time constraint.
You probably will need to give a little more leeway on the timeout at the server than on the client, maybe a few seconds, to account for network delays, otherwise the user might see one second remaining, click submit, and then by the time the server request is received, it will seem like the timer has expired.
Be sure to add require 'digest/md5' to get access to MD5.
Using MD5 signatures like this is a great way to verify that a client has not altered key parameters in a form in a stateless manner, whatever you would like them to be. A good addition to your bag of tricks.
Good luck!
There's no 100% tamper proof way of implementing this since you would need to do this using JavaScript which can be turned off or manipulated by a sufficiently malicious user.
However if you aren't concerned about these issues you could simply set a timeout on the page to submit the form after the number of seconds have elapsed. To do this you would need something similar to the follow. Obviously timeInMilliseconds would need to be generated into the page from the template on the server side.
window.setTimeout(function() {
document.forms['survey_form'].submit();
},
timeInMilliseconds);
Create model for ongoing surveys, and add after_create filter that will set deadline to Time.now + survey_duration. Keep logic that will deny late sending of answers in model.