¿Cómo usar Ajax para pasar variables de Javascript a Coldfusion?

I need to use Javascript to detect the local users UTC offset and then use that UTC offset in my cfquery's for the rest of my large website.

Here is a top down logic that I'd like to run. I just need help getting the Javascript data (UTC offset) from Javascript set into a Coldfusion variable so that it can be used in the SQL in my cfquery's.

1.) Page loads 2.) Ajax calls simple Javascript to get the users UTC offset 3.) UTC offset is returned in a Coldfusion variable 4.) Coldfusion variable is used in SQL cfquery to adjust UTC dates to accommodate the users timezone.

preguntado el 22 de mayo de 12 a las 17:05

Echa un vistazo a mi respuesta a una pregunta similar: stackoverflow.com/a/9541365/808921 -

unfortunately I can't use session variables in our shared hosting web environment. Any other suggestions? -

The fact that my other answer refers to a session structure doesn't change the basic approach. Take another look. -

2 Respuestas

One possible way is to use JavaScript to set a timestamp cookie on the first page they hit. Then in subsequent pages reference that cookie with cfcookie.

var curDateTime = new Date();
var tzoffset = "-" + (curDateTime.getTimezoneOffset()/60);
var expiredate = new Date();
expiredate.setTime(expiredate.getTime()+(120*24*60*60*1000)); // set expire date to 120 from now
document.cookie = "local_tz="+tzoffset+";expires="+expiredate.toGMTString();

contestado el 22 de mayo de 12 a las 18:05

Thank you but I was hoping to avoid cookies and use an Ajax solution if possible. - AARON MCLEAN

How reliable does this have to be? You could save the user offset in a database table along with the clients IP address. It would create some overhead since you'd be running a UDF/query on every page request. Not recommended or best pratice, but it could work for your particular setup. - Sweyrick

  1. First get the offset and send to coldfusion cfc (assuming your using jquery js library)

    var offsetmin = new Date().getTimezoneOffset(); //in minutes var offsethr = offsetmin/60; //hours

     type: "POST",
     url: WebRoot + "/pathtocfc.cfc?method=createClientOffset",
     data: "offset="+offsethr,
     dataType: "json"
        //make sure you saved it successfully with your client ip address


  2. Next save offset and ip in coldfusion

    <cffunction name="createClientOffset" access="remote" returnformat="JSON">
    <cfargument name="offset" >
    <!---get ip address from cgi scope--->
      <!---try to save client ip and offset together using cfquery--->

3.On subsequent page requests call a UDF to try and get the client offset or make the call before you run your query to return dates and subtract utc offset from gmt date.

    <cffunction name="readoffset" access="public">
    <cfargument name="ipaddress" >

    <!---use cfquery to match ipaddress with offset if it doesn't exist then use a default offset or add logic to create one if it doesn't exist--->

      <cfreturn offset />

contestado el 23 de mayo de 12 a las 01:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.