Skip to main content
Inspiring
December 20, 2011
Question

How to pass values from ColdFusion to JavaScript function

  • December 20, 2011
  • 4 replies
  • 7502 views

Hi there,

I need to return the current time on the server in the format:

<Short month name> <day of month>, <year> <hours>:<minutes>:<seconds> <timezone>

to a JavaScript function in html page.

Any idea how to do this using ColdFusion.  My server is running ColdFusion. 

Thanks,

Joe Green

    This topic has been closed for replies.

    4 replies

    BKBK
    Community Expert
    Community Expert
    December 20, 2011

    GreenGoneMad wrote:

    Hi there,

    I need to return the current time on the server in the format:

    <Short month name> <day of month>, <year> <hours>:<minutes>:<seconds> <timezone>

    to a JavaScript function in html page.

    Any idea how to do this using ColdFusion.  My server is running ColdFusion. 

    A solution is possible. However, we first have to know how you intend to convey the information to the html page.

    Inspiring
    December 21, 2011

    BKBK my question is how to convey that information to html page. I don't know that.

    My html page has a dountdown counter and I need to provide it with server date and time. Currently my timer is using client side date and time.

    Community Expert
    December 21, 2011

    My html page has a dountdown counter and I need to provide it with server date and time. Currently my timer is using client side date and time.

    1. Write a CF script that generates AJAX-compatible output (JSON or XML).

    2. Add an AJAX call to that script within your HTML page.

    Dave Watts, CTO, Fig Leaf Software

    Dave Watts, Eidolon LLC
    Community Expert
    December 20, 2011

    JavaScript is just text, like HTML. You can build it directly like you do HTML:

    <script>

    var someJSvar = '<cfoutput>#yourCFvar#</cfoutput>';

    </script>

    Dave Watts, CTO, Fig Leaf Software

    Dave Watts, Eidolon LLC
    12Robots
    Participating Frequently
    December 20, 2011

    So how do you want to do it?  Do you just want to do it once when the page loads?  Or do you need to do it regularly without reloading the page? The latter will require an Ajax call, the former can be done simply by outputing what you want with cfoutput and the appropriate date format in a JavaScript block to set a variable.

    You need to be more specific about what you want. There are probably half a dozen ways it could be done.

    Jason

    Inspiring
    December 20, 2011

    either the <cfwddx> tag or toScript() function will do that for you. 

    Inspiring
    December 20, 2011

    The JavaScript function resides in a .html page and so I cannot put the coldfusion function toScript() in the same html page. I will have a seperate Coldfusion page that will pass the current server time in a specific format to another .html page.

    I need to grab current server time when the page loads.

    12Robots
    Participating Frequently
    December 20, 2011

    Then you will need to do it with an Ajax call. Or rename the file to index.cfm and have it execute as a ColdFusion template.

    Jason