greetings all
i was wondering if it’s possible to send some javaScript code in the response of a controller
meaning that i want to invoke a javaScript but not from the jsp (from server side)
something like:
var d = new Date();
var gmtHours = -d.getTimezoneOffset()/60;
var tz=gmtHours;
window.location="page?tz="+tz;
what do you think guys ?
There are two parts to the answer:
Executing JavaScript when the answer comes back from the server. To do that you can wrtite a Javascript in html, something like
usually people have something like
It will execute your function when windows loads.
It looks like you want to know the timezone for the user you are displaying results to. As mentioned, HTTP headers don’t have this information, so you need to submit it or store as user preference.
If you don’t want to store it, you either need to add it to every submit or URL. This way you will always have timezone on server side and you don’t need to do 2 round trips.
The workflow might look like this:
1. User submits form or clicks a link -> 2. you form the results on server side -> 3. display results on client side.
Looks like you want to show some dates/times specific to timezone. If you send data to the client using ajax, you can then get it in javascript before step 3 and change it according to your preferences.
If you want to do all the formatting on server side you need to submit the timezone with the original request in step 1.
You can add 2 more steps 1.2 submit timezone request as a response to user action. 1.3 Re-submit the request with timezone info using javascript. This is not optimal, but could work.