Skip to main content
July 1, 2010
Question

Convert Javascript variable to coldfusion variable

  • July 1, 2010
  • 2 replies
  • 9480 views

Hi,

Can anybody assist me how to convert a javascript variable to Coldfusion variable.

What i m trying is,

<script>
var manasa = "hello world";
</script>

<cfset myVar = assign above js variable value here>

<cfoutput>#myVar#</cfoutput>

Thanks in advance,

Regards,

Manoz

    This topic has been closed for replies.

    2 replies

    Participating Frequently
    July 1, 2010

    I had a opposite thing to do like passing Cf variable to JS

    I did something like this....

    <SCRIPT type="text/javascript" LANGUAGE="JavaScript">
    <cfoutput>
       var #toScript(Application.Host, "App_host_var")#;
      </cfoutput>

      </SCRIPT>

    Now the Variable App_host_var can be used in

    'http://'+App_host_var+'/'

    Hope this could help you ....

    Inspiring
    July 1, 2010

    That's not "passing a CF variable to JS", it's simply writing out JavaScript with CFML.  JavaScript is just text... your CFML code is just generating text that the client will process as JavaScript.  CF is not "interacting" with JavaScript there.

    And the answer to your original question remains the same as per my initial reply: to get data from JS to CF, you need to make an HTTP request from the client, passing the relevant data as part of the request.  That is the answer... and as it stands with HTTP it's the only answer there is, and the answer won't change if you explain your situation some more ;-)

    What you probably need to do is to google "AJAX" and "JQuery".  And read up on how to make HTTP requests with JS.

    --

    Adam

    Inspiring
    July 1, 2010

    A request gets processed like this:

    Client PC requests a document

    Web server receives requesty

    Web server sees it's for a CFM URL, and passes it to CF

    CF processes the request, generates document (which is text... HTML, JS, CSS etc)

    CF passes document back to web server

    Web server returns doc to client

    Client renders doc & executes scripts

    So... CF runs on the CF server, and JS runs on the client machine.  And there's quite a "gap" between when CF does its bit and JS does it's bit.

    In short: you simply cannot do what you want to do.

    The only thing you can do is to send a request from the client machine back to the web server, with the value you want as some sort of parameter (URL or FORM), which CF can then do something with.

    But CF & JS do not talk to each other.

    --

    Adam