4 Replies Latest reply on Jan 15, 2007 10:46 PM by BKBK

    set client var using javascript

    tragik
      i have a form where a user enters a line of info, and i want to save that as a client variable without having to post the form. is it possible to set a CF client variable using javascript?
        • 1. Re: set client var using javascript
          BKBK Adobe Community Professional & MVP
          ... > i want to save that as a client variable without having to post the form.
          > is it possible to set a CF client variable using javascript?

          No. Coldfusion can only see data that the browser sends to it. You may use Javascript, but it, too, has to send the data to the Coldfusion server.

          • 2. Re: set client var using javascript
            tragik Level 1
            i have no problem having js/cf talking to each other with ajax, however a js change cant be saved in a cf component unless its during page creation.

            cookies can be created/edited/deleted using js, so i just need to know how i would go about editing a cf created cookie using js.
            • 3. Re: set client var using javascript
              tragik Level 1
              .i used javascripts document.cookie to make changes to the correct coldfusion client variables..works like a charm :)
              • 4. Re: set client var using javascript
                BKBK Adobe Community Professional & MVP
                > cookies can be created/edited/deleted using js, so i just need
                > to know how i would go about editing a cf created cookie using js.

                You've inched away from your original question. It was about how to use Javascript to save form data in Coldfusion without having to send the form from the browser. The answer is, you can't.

                > i used javascripts document.cookie to make changes to the correct coldfusion
                > client variables..works like a charm

                You've not changed any Coldfusion variable at all. What you've simply done is use Javascript to write a new cookie, which Coldfusion then reads later. To see that, do this test.

                Save and run the following code as the page testCookieJS.cfm

                <html>
                <head>
                <title>testCookieJS</title>
                <script type="text/javascript">
                document.cookie="JS test cookie";
                </script>
                </head>
                <body>
                Coldfusion cookie struct dump:
                <cfdump var="#cookie#">
                </body>
                </html>

                You will find that the "JS cookie" is not dumped. However, you will find that the cookie is actually present in the browser.

                Now, close the browser and delete all cookies. Save and run the following code as the page testCookieCF.cfm

                <cfcookie name="CFCookieTest" value="CF test cookie">
                <cfdump var="#cookie#">

                You will find that the "CF cookie" is dumped. The difference in the result of the two tests shows that one cannot use Javascript to change the cookie that Coldfusion sets. In fact, generally speaking, you cannot use Javascript to modify any Coldfusion variables. If you could, anyone would be able to use an HTML page to rewrite your Coldfusion site.