> -----Original Message----- > From: Jenny Gavin-Wear [mailto:[EMAIL PROTECTED] > Sent: Saturday, August 02, 2008 2:12 AM > To: CF-Talk > Subject: Javascript and Coldfusion > > I'm looking for some good advice/info on exchanging variables between > CF and > javascript, anyone know of a good reference site, please?
That's a very (VERY) big question. ;^) There are many ways to accomplish this, from the relatively simple to the incredibly complex. What system you might use is really up to personal preference and your needs. Before any of this you really need to understand (and maybe you already do, so ignore the next bit) the data flow involved, the paths of information and so forth. A normal transaction ("getting a web page") might look like this: 1) User clicks a ColdFusion link in a browser which submits a request. This request might contain URL (GET) or Form (POST) variables. It would also contain any cookie variables related to the domain. 2) The web server gets the request (at this point it may actually change it, but usually doesn't) and gives it to ColdFusion to process. 3) ColdFusion processes it. It can use the variables that might be contained in the request and access any domain cookies. It can also store information "permanently" in many ways (but usually in the Session scope). 4) ColdFusion generates a response - usually an HTML page although using the CFCONTENT tag it can return any type of date (images, flash objects, style sheets, etc). It can also generate (but not run) JavaScript (technically any client-side script). 5) ColdFusion gives the response back to the web server - it's done with this request. The web server has the option of changing it here (usually to add universal information like CPP cookies or for performance reasons, like compressing the return). 6) The web server gives the response back to the browser - it's done with this request. The browser parses the result and displays the results. If the return is an HTML page or Javascript file the browser will run that script. Any script could in turn create, access or manipulate GET or POST variables or cookies. It could itself initiate a request to a new ColdFusion page (without the user needing to do anything) and the whole process starts again. There are a few point here related to your question: 1) Both CF and JavaScript can access and use GET, POST and Cookie variables - but not at the same time. 2) A single JavaScript can make MANY requests to ColdFusion, but a ColdFusion page can only respond once. In short this means that a single web page might have a continuing conversation with ColdFusion but each exchange must be done via a new ColdFusion page. 3) To maintain information across a conversation you need to store information state. In both CF and JavaScipt this can be done using Cookies. In CF you have many options (Session, Server and Application scopes, database, etc) while in JavaScript you're more limited. JavaScript can, of course, store information within itself, as local variables (for this reason it's usually easiest for one script to make multiple requests to ColdFusion) but you might also store information in another script maintained in a "permanent" location like a frame. So... By far the simplest way to trade information between CF and JS is the basic, standard request. The page is displayed, JS can modify information, the user clicks a link or submits a form and CF see the data. This is the way 99% of the web works - click, response, click, response. I'm almost certain what you're talking about however is passing information between JS and CF without refreshing the page - without the user's needing to make a specific action. I think the most straight-forward way to do this is by using images and cookies. This is a very old technique - dating back to at least 2000 - but it still works just fine for small pieces of information. The basic idea is that JavaScript adds information to an image URL then calls to change that image. Instead of calling an image directly however it calls a CF file which uses the information, puts a response in a cookie and then sends the originally requested image. JavaScript can then read the cookie to get the response. I wrote an article about this in late 2000 but several people came up with the idea independently about the same time. The article's old, but it still holds up pretty well: http://www.depressedpress.com/Content/Development/JavaScript/Articles/GIFAsP ipe/Index.cfm Today most of this kind of functionality falls under the sadly named "AJAX" umbrella. This nearly also involves using a relatively recent piece of browser functionality called "XMLHTTPRequest" which allows JavaScript running in a browser to make direct HTTP requests to the server the current page was delivered from. There are many, many (MANY) resources dealing with AJAX. There are also a large number of abstraction libraries available which make working with it easier. These libraries tend to focus on a specific technology but in the end produce JavaScript and some server-side script. For example the Google libraries allow you to produce both the Server and Client-side code using only Java. The Yahoo "YUI" library simplifies your JavaScript as do many popular JavaScript libraries. There are several ColdFusion libraries that let you do nearly all your work in CF. Which of the many options available would be best will really decide on what you're trying to do, your skill level and requirements. Jim Davis ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to date Get the Free Trial http://ad.doubleclick.net/clk;203748912;27390454;j Archive: http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:310110 Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4