4 Replies Latest reply on May 4, 2006 6:55 AM by DJ_Jamba

    javascript to cfscript

    DJ_Jamba Level 1
      Hello Gurus
      Wondering if this is possible?
      (CFMX 5)

      I have a cfscript function:
      <cfscript>
      function doSomething(thingToDo) {
      return URLEncodedFormat(Encrypt(thingToDo,myKey));
      }
      </cfscript>

      takes 'thingTodo', encrypts it with private key and then makes friendly to use over URLs.

      Now I want to pass in a JS variable instead of a raw string, something like this:

      <cfoutput>
      <select name="selectObject" onchange="javascript:location.href='somepage.cfm?#doSomething(this.value)#'">
      <option value = "option1">Option 1
      <option value = "option2">Option 2
      </select>
      </cfoutput>

      so that thepage forwards to:
      somepage.cfm?with_encrypted_js_variable_here

      Is there a way to do this?
      I have no hair left, so any pointers in the right direction appreciated

      Jam
        • 1. javascript to cfscript
          James74
          Couldn't you just get rid of the CFScript function and on your onchange do:
          location.href="somepage.cfm?#UrlEncodedFormat(Encrypt(thingtodo,myKey))#

          Or, I can't remember if this is the correct syntax, but set a variable equal to your function call
          and append that to your url:

          <CFSET EncString = doSomething()>
          location.href="somepage.cfm?#EncString#
          • 2. Re: javascript to cfscript
            cfpb Level 1
            Not sure if this is what your looking for but... how about this?

            Note: if you want to encrypt the values on the fly you'll need to encode ( or encrypt ) through javaScript. I beieve the cold fusion and javaScript are processed and run at different times.

            <cfscript>
            myKey = 'test';
            function doSomething(thingToDo) {
            return URLEncodedFormat(Encrypt(thingToDo,myKey));
            }
            </cfscript>

            <script language="javascript">
            function loadURL(str) { self.location.href = 'somepage.cfm?' + str; }
            </script>

            <cfoutput>
            <select name="selectObject" onchange="loadURL(this.value)">
            <option value = "#doSomething('option1')#">Option 1 </option>
            <option value = "#doSomething('option2')#">Option 2 </option>
            </select>
            </cfoutput>
            • 3. Re: javascript to cfscript
              DJ_Jamba Level 1
              Thanks guys - some good responses.
              Just to clear up the typo at the top of my post : (CFMX or 5)

              I'm gonna try both ideas in the morning so should reply early afternoon (GMT)

              Art first glance:
              james - i think the cf code gets processed first so I'm gonna place the js function at the point it's called and play around there.
              cfpb - this looks possible. My problem is I was trying to eval the js var while inside a cf evaluate (urghhh) but your solution places the call on the option value (which incidentally is produced via cfloop). I cant believe i didn't think of this!

              P

              thanks for your help and i'll report back later..
              • 4. Re: javascript to cfscript
                DJ_Jamba Level 1
                cfbp - excellent! Your suggestion worked fine (even on the fly!)
                Thanks for all your help

                Jam