ColdFusion Components and Var Scoping Trick
Var scoping is essential when dealing with ColdFusion Components. You can get weird results or even some hard-to-track errors if you don't do this. I'm not privy as to why this happens, I just know that it does. It's one of those mysteries that I just never bothered to research. All I know, is that sometimes it's a pain in the butt, especially when your writing a fairly complex function and you forgot to var scope an "i" in a cfloop or some other quick and dirty variable you made.
Well a few months back, I started doing something with my function variables, that I thought I would share. Not only does it solve my var problem, but it winds up making the top of my function shorter (I'm a stickler for cleanish looking code). Essentially I create a generic local struct in the begining and I'm sure to var scope it, like so:
<cfset var local = StructNew() />
Reason #592 why I hate Internet Explorer
My last step on any website design is to go through it and make sure that it works and look correct in the various flavors of Internet Explorer. I can't really say that the time spent doing this is blissful. I compare it to going to the dentist, because I never know what I'm going to find and when I do it's rarely good.
So today as I was debugging, what I thought was a very simple, JavaScript page. I came across one of those nifty "This method or function is undefined" errors from the Internet Explorer debugger. And as we all know, the IE debugger is as worthless as playing "I Spy" with a blind person.
Gracefully timeout a users session, with CJ Session Timeout!
I was running into a little problem with a website of mine that was experiencing some weird errors. The site has a password protected directory that allows the users to edit upcoming events. The problem was that this site was on a shared hosting environment and I didn't have control over the session timeout length. They had it set at 30 minutes and it sometimes took people longer than this to complete their edit (or maybe they took a phone call). When they tried to enter the event, they would get redirected to an error page without really knowing why.
I made a minor update the the the CJ Session Timeout JS script. You can now pass settings to the script while you are initializing it.
The new update can be found on the original blog post, which you can find here.
JSLint Keeps Warning Me About Calling a Function Within A Loop
I use JSlint to check my JavaScript on a regular bases, most of the time it just for light scripts that I just need to check to make sure that I don't have a typo. But occasionally I check more robust scripts and I find myself presented with an error message that says "Problem at line XXX character XX: Be careful when making functions within a loop. Consider putting the function in a closure".
I know I've come across this in the past and I know that I fixed it, but the problem is I can never remember what the heck I did to fix it. Then I remembered that I have a blog! I can just put it there and never have to search far for the answer ever again!
Here's the problem:
function myHappyFunction() {
var a = document.getElementsByTagName('a');
for(var i = 0; i <= a.length - 1; i++) {
a[i].onclick = function(e) {
alert('Ouch!');
};
}
}