ColdFusion Tips and Tutorials

ColdFusion Tips and Tutorials. Tips on ColdFusion, AJAX, CSS, JavaScript, HTML, Design, and more.

CFUnited Developer Conference 2010
Use this code TIPSCUST to get $100 off your registration @ CFUnited! We'll see you There!


ColdFusion Tips
Page 1 2 3
148 ColdFusion, Ajax, FuseBox, Tips, and Tut
147 Included Point of View
146 Javascript - OnFocus
145 Nathan's Rules of Professional Web Desig
144 Universal Server-Side Check | Bandwidth
143 Meeting Schedule | Identification Sessio
142 Breaking Frames Without Javascript
141 Unreal Forms
140 Screen Resolution
139 Human Help
138 Better Server-Side Validation
137 Automatic Server-Side Validation
136 Regular Expression Laboratory,ColdFusion
135 Rank-Ordered Site Search
134 Building Cryptograms
133 Well-Formed Includes
132 Grouping Families for Visits
131 Display Families on a Map Grid
130 Slide Shows
129 Determine Your Database Engine
128 ColdFusion in Context: Maxlength Lies
127 Something Extra
126 Parsing Database Structure from Data Def
125 Valid Values Maintenance
124 Print 1
123 Hide Session Id
122 Downsizing Data to Access
121 Time to Load a Page, FuseBox 4
120 Order and Rank by Subset
119 Warn through E-mail & Update on Paste Sp
118 Paste Spreadsheets, ColdFusion Component
117 Review Files Having Fixed-Length Fields
116 Organized Help
115 Sequence Slider
114 Bad Bits
113 Logical Deduction
112 Whiteout
111 Forced Navigation
110 Managing Permissions
109 Time Travel
108 Test First
107 Get Distance Between Map Coordinates
106 Validating Checkboxes
105 Matrix Manipulation
104 Field Help
103 Fake Object Not Found
102 Rank Order Correlation Coefficient
101 From Calling Pairs to Calling Tree
100 Posting Notice
99 Logout Persuasion
98 Release Session Memory
97 Use Identically Named Fields
96 Web Bug
95 Password Generation
94 Core Queries
93 Use CFFTP
92 Insert, Update, and Delete
91 Stack
90 T-Value
89 Bulk Data Entry and E-mail Validation
88 Quick Reset
87 Design 1
86 Use CFFTP
85 Support Login with AutoPost
84 Login and Site Protection
83 XY Graphs in a Graphing Calculator
82 Read Encrypted Files
81 Showing Progress
80 Frugal Cross-Browser Javascript
79 Tabbed Folders
Page 1 2 3



Custom Search
ColdFusion TIPS PLUS


Issue 00080 http://www.cftipsplus.com

I. My Comments

II. ColdFusion In Context: Frugal Cross-Browser Javascript
By R. Martin Ladner
martin.ladner@knology.net



In the Next Issue:
ColdFusion In Context: Showing Progress



I. Comments:

Moved in I now have a Internet Connection at my new home... if I can find it in the boxes.


Keep Coding,
Nathan Stanford
President/CEO
C.F. Concepts, Inc.
http://www.coldfusionmonthly.com
http://www.cftipsplus.com

If you have suggestions for articles send them to us.
If you would like to write for cftipsplus.com
send us an email to:

admin@cftipsplus.com

IF YOU WANT TO BE AN AUTHOR SEND IN YOUR COLDFUSION TIPS.

Remember this is a great way to get your name known in the
ColdFusion Community.



Advanced, Intensive ColdFusion Training!

Visit this site. If you have plans to get training here is a company that provides Advanced, Intensive ColdFusion Training. Check them out.

http://www.coldfusiontraining.com/index.cfm?ref=cftipsplus



CFM - The ColdFusion Monthly!
http://www.ColdFusionMonthly.com

By becoming a .CFM subscriber, you'll receive. ColdFusion information you will not find anywhere else:
Tips, tricks, techniques, strategies, and a wealth of knowledge you will be able to apply immediately to your work, all from established developers.

Monthly access to new articles written by industry-leading ColdFusion experts. A fully-functional application that accompanies each issue.

Full access to all issues, past and present (with a one year paid subscription). The chance to be seen in our "Community Spotlight" corner, which showcases new writers and emerging talent


If you have any suggestions please email me at cftips@nsnd.com.


II. ColdFusion in Context: Frugal Cross-Browser Javascript
By R. Martin Ladner
martin.ladner@knology.net


Suppose you're using browser-specific javascript but want to minimize the amount of code sent to the client browser. The classic solution is to send the client the javascript for all browsers and select the correct code when it gets there. However if your application paid attention to browser characteristics during login, you might be able to send only the version needed for this client's browser throughout the rest of your application. That's what this tip is about. Along the way, you'll review how to use javascript to push forms around and do some event-handling in javascript.

Overview
The application will need to test the browser during login, tell ColdFusion the results, have ColdFusion remember the results, and have ColdFusion generate just the javascript the client needs. During login, test for the browser characteristics you need to know. You could write to a form variable, URL, or cookie to tell ColdFusion the information it needs to know. Since the login page feeds a form anyway, this example will change a form variable for simplicity. The authentication page will store the browser information as a session variable. When the user goes to subsequent pages, if the session variable doesn't exist, the user is directed back to the login page. If it does exist, ColdFusion selects the appropriate javascript based on the original test at login.

Login
Here's code for a simple login; call it login.cfm. It removes ColdFusion's session ID cookie. (For reliability, always use a value when deleting a cookie.) It provides a simple login form. It uses a simple test to get the browser family. There are published tests that run for hundreds of lines and pick up many nuances about a browser. You could easily afford to use a complex test at this point if you were following this tip; because, you'd only need to run it once per session. When the page has finished loading, the javascript test runs and changes a hidden field of the form; you'd use multiple fields in real life: one for each browser characteristic you want to remember.

<cfcookie name="CFID" expires="-1" value="dummy">
<form action="card.cfm" name="check" method="post">
Enter employee ID starting with "f"; others fail:
<input type="text" name="employeeID" value="" size="10" maxlength="8">
<input type="hidden" name="bfamily" value="">
<input type="submit" name="submit" value="OK?">
</form>
<script language="javascript">
if (navigator.appName == 'Netscape')
document.check.bfamily.value = 'NN';
else document.check.bfamily.value = 'IE';
</script>

Check Credentials
Create a page that checks credentials; call it card.cfm. It begins by defining two forms that the user will never see. To avoid confusion in this small example, it uses forms instead of the dreaded cflocation tag to change the current page. If the cflocation tag is used, then 1) the CFID cookie doesn't get deleted when the user is bounced to the login, and 2) the URL window of the browser continues to display "case.cfm" instead of "p/inside.cfm". (It still goes there, but it doesn't tell you that.) Using a form sidesteps these problems. If the forms don't have at least one field, Netscape will ignore them. If the forms don't have a submission method of "post", Netscape will display the field in the URL. That's why each form has a submission method and a dummy field.
If the employee ID starts with "f" - insert your real test here - it sets session variables and advances the user to the inside of the application: inside.cfm in directory "p" (for "production"). If it's not, it dumps the user back at the login screen. Enabling session management lets ColdFusion handle the rest automatically. In real life, you'd give session.OK a time-based secret relationship with the session ID to discourage tampering.


<form name="forward" action="p/inside.cfm" method="post">
<input type="hidden" name="dummy">
</form>
<form name="backward" action="login.cfm" method="post">
<input type="hidden" name="dummy">
</form>
<cfapplication name="frugalJS" sessionmanagement="yes">

<cfif ucase(left(form.employeeID,1)) eq "F">
<cfset session.empID=form.employeeID>
<cfset session.OK="asdf">
<cfset session.bfamily=form.bfamily>
<script language="javascript">
document.forward.submit();
</script>
<cfelse>
<script language="javascript">
document.backward.submit();
</script>
</cfif>

Block the Production Directory
You'll use Application.cfm to block the "p" directory. This code automatically becomes part of every cfm page in the directory. Create the directory and put this code in Application.cfm inside it. This code enables session management and tests to see that the user is logged in. Whatever formula you use to assign session.OK must match the test you use on this page. If the user isn't logged in, bounce the user to the login screen.

<cfapplication name="frugalJS" sessionmanagement="yes">
<cfif not ( isDefined("session.OK") and (session.OK is "asdf") )>
<cflocation URL="../login.cfm">
</cfif>

Work Inside the Application
Within the application in the p directory, use the browser data recorded during login to pick browser-dependent javascript to be sent to the client. The session variable bfamily tells ColdFusion which code to use; the other code won't reach the browser. This code is odd. It lets you type outside the box but puts the results into the box. Call it inside.cfm.

If the browser is Netscape Navigator, then each keypress has to be captured and fed to a regular function in order to be used. The "captureEvents" code does this. The code that depends on it uses the "which" function to get the ASCII value of the character, then converts that code to the character itself. Finally, it adds the character to the current value of the field.

If the browser is not Netscape Navigator, the code assumes it's IE. then each keypress has to be captured and fed to a regular function in order to be used. The "onkeypress" characteristic of the document can be used directly. The rest is similar to the code for Netscape.


To see a function dependent on <cfoutput>#session.bfamily#</cfoutput>,
click and then type OUTSIDE the box.<br>
<form name="toss">
<input type="text" name="look">
</form>
<script language="javascript">
<cfif session.bfamily is "NN">
function myFun(myEvent) {
var keyCode = myEvent.which;
var keyChar = String.fromCharCode(keyCode);
document.toss.look.value += keyChar;
}
if (document.captureEvents) document.captureEvents(Event.KEYPRESS);
document.onkeypress = myFun;
<cfelse>
function document.onkeypress() {
var keyCode = window.event.keyCode;
var keyChar = String.fromCharCode(keyCode);
toss.look.value += keyChar;
}
</cfif>
</script>

Try It Out
Now to see the results. Go to login.cfm and try a bad login. Then, enter p/inside.cfm directly to watch the code throw you back to login.cfm. Try a good login and watch as it brings you through card.cfm to p/inside.cfm. Click and type outside the box to prove the browser-dependent code is working. View source to prove that only the appropriate code is reaching your browser. Then try the whole thing again with the flavor of browser you didn't use the first time.
You can surely extend this technique. See if you can keep Netscape from displaying the words "Transfer interrupted" when card.cfm forces the browser to a new page. Perform a more detailed browser test at login. Build your application. Then, tell us what you've learned.
=Marty=


Publisher and Creator:
Nathan Stanford,
admin@cftipsplus.com
C.F. Concepts, Inc.
http://www.cftipsplus.com

Macromedia and ColdFusion are U.S. registered trademarks.


Copyright (c) 2000 - 2001 C.F. Concepts, Inc.
CFTIPSPLUS.COM and NSND.COM

Permission is granted to circulate this publication via
MANUAL forwarding by email to friends provided that the text is
forwarded in its entirety and no fee is charged.

Photo of Nathan Stanford
Nathan Stanford
LinkedIn

R. Marty Ladner's
Site