Skip to main content
June 29, 2009
Answered

javascript inside cffunction

  • June 29, 2009
  • 2 replies
  • 5116 views

I have a really stupid question, but I need to use some javascript in a cffunction in a cfc.

Is this possible?

I am trying to use google maps geocode to get the latitude and longitude for addresses and it works in a cfm page, but doesn't in the cffunction.

the below converts the data to json and sends it to a cfc, the cfc then write the data to a file.

the overall goal is to call the service and get this information back in cfml if all possible. we are using google maps api v3.

here is my code:

testget.cfm

<cfajaxproxy cfc="cfc.mycfc" jsclassname="gs">

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">
  
  
   var geocoder;

     
       geocoder = new google.maps.Geocoder();
       var address = 'your street, your city, your state, your zipcode';
      
    geocoder.geocode( {address: address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK && results.length) {
     // You should always check that a result was returned, as it is
     // possible to return an empty results object.
     if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {

      var e = new gs();
      var jsonObj = ColdFusion.JSON.encode(results[0].geometry.location);
      var json = e.updateLatLng(jsonObj);

     }
      }
    });

</script>

---------------------------------------------------------------------------------------------------------------------

mycfc.cfc

<cffunction name="updateLatLng" access="remote" >

<cfargument name="latlng" >

<cfset i = deserializeJSON(latlng) >

<cfset lat = i.Yd />

<cfset lng = i.$d />

<cffile action = "write"

file = "C:\ColdFusion8\wwwroot\dev.zipoodle.com\jsontest.txt"

output = "cfclat = #lat# and cfclng = #lng#" >

 

</cffunction>

    This topic has been closed for replies.
    Correct answer ilssac

    LinkMc wrote:

    I have a really stupid question, but I need to use some javascript in a cffunction in a cfc.

    Is this possible?

    No it is not possible, at least not the way you are probably trying to do it.

    ColdFusion runs on the server, JavaScript runs on the client and never shall the two meet.

    You can create CFML that dynamically builds javascript even javascript to be used with google maps.  This is very powerful and I do that very thing all the time.

    You can create JavaScript that makes HTTP requests to CFML pages and gets data back either by manipulating the browser, DOM or XMLHttpRequest Object.  The former is often called DHTML and the latter is often called AJAX.  Both of these are also very powerfull techniques that can create highly dynamic browser based applications.

    Both of these concepts require a clear understanding of what is happening on the server and what is happening on the client and how data may be passed between one system and the other.

    2 replies

    ilssac
    ilssacCorrect answer
    Inspiring
    June 30, 2009

    LinkMc wrote:

    I have a really stupid question, but I need to use some javascript in a cffunction in a cfc.

    Is this possible?

    No it is not possible, at least not the way you are probably trying to do it.

    ColdFusion runs on the server, JavaScript runs on the client and never shall the two meet.

    You can create CFML that dynamically builds javascript even javascript to be used with google maps.  This is very powerful and I do that very thing all the time.

    You can create JavaScript that makes HTTP requests to CFML pages and gets data back either by manipulating the browser, DOM or XMLHttpRequest Object.  The former is often called DHTML and the latter is often called AJAX.  Both of these are also very powerfull techniques that can create highly dynamic browser based applications.

    Both of these concepts require a clear understanding of what is happening on the server and what is happening on the client and how data may be passed between one system and the other.

    Known Participant
    June 30, 2009

    Yup i agree this is what i came across: http://www.edwardbeckett.com/Blog/index.cfm/2009/4/14/Google-RESTful-Ajax-raquo-JSON-Search-in-ColdFusion its server-side google talk, the dude is using coldfusion already so it should be an easy transition.

    Buzz.

    June 30, 2009

    Thanks for the info guys, I guess I need to rethink this through, I need to do this from a cffunction in a cfc.

    What way would be the best to do this from a cffunction in a cfc?

    I was hoping Ian, was going to say something like, "yes, you can do that"... but I knew deep down that ColdFusion process before the page is display, it actually creates that html (tmp) then the javascript kicks in. I guess this is really fustrating.

    June 30, 2009

    I hate to do this, but I really need help on this...

    *bump*