surelyyourenotserious.com
Event Function Stacking in JavaScript

<disclaimer>I don’t usually post the intricacies of my work here, but this one was just too cool to keep to myself. Those of you who are not steeped in JavaScript, DOM, and XHTML can stop reading here and come back later for your normal dose of humor and/or world news.</disclaimer>

I’ve been struggling for a while now with a problem in IE. On my company’s website, we use the DOM and JavaScript perform a lot of CSS trickery. IE doesn’t support some of the CSS pseudo classes we use to decorate our forms and to create our drop-down navigation. The best way I’ve found to get these pseudo classes to work in IE is the suckerfish library. You can read more about how suckerfish accomplishes this at the son of suckerfish site. They explain it much better than I could here.

Suckerfish has been wonderful and we’ve used it all over our site. However, I ran into a snag. We’re adding a search form to the top of our page template, where real estate is premium. In order to save space, I created a small icon-like button to submit the form rather than using a bigger “Search” button.

Now, to be a good and accessible site, we need to explicitly label the form somehow so the user doesn’t have to infer what it’s for. My idea (admittedly stolen) was to have the default value of the field be "Search...". When the user clicks into the field, an onfocus event removes the default value, and if the user doesn’t give us a search term, the onblur event puts the default value back. Pretty straight forward, right? Wrong.

Suckerfish has this one drawback: It overwrites any preexisting event. When my suckerfish script added an onfocus event to my search box (to change the CSS style) it overwrote my clever text value swap. I needed a way for suckerfish to stack events instead of blindly assigning the event listener to a new function.

Another bit of code we use (to created rounded boxes) utilized Scott Andrew’s addEvent method. I tried switching suckerfish to use this, but the outcome was the same. The inline event functions were overwritten.

My good friend and colleague, Randy Peterman, sent me a link to a potential fix. Simon Wilson’s addLoadEvent method seemed to do exactly what I was looking for. And sure enough, I was able to stack my events by slightly modifying his window.onload logic to take the element object and the event type as arguments. Cloogy, yes. Uses eval method, yes. But worked, apparently, at least in testing. It wasn’t until I added my code to change the value of this that this method failed.

It took a me a while to really understand what Simon’s method was doing. It involves JavaScript Closures and, what I best understand as the scope of nested function objects. When using Simon’s closure method, the this object reference gets lost as the nesting of functions unwinds. So when I tried to nest…

if( this.value == 'search...')

…I found out that this was undefined. The complicated solution was just too complicated. I needed to dumb it down.

So I did just that: Make it elementary. Rather than storing function objects within function objects and nesting closures, I just needed to strip out the meat of the functions and tack them together. Sounds pretty sophomoric, but it solved my problem.

See a sample here.

/* Retains any existing event listener, rather than overwritting it. Trint Ladd */
function stackEvent( obj, eventType, func )
{
	var oldEvent = eval( "obj." + eventType );
	var oldInnerSource = "";
	var newInnerSource = func.toString()
	newInnerSource = newInnerSource.substring( newInnerSource.indexOf( "{" ) + 1, newInnerSource.lastIndexOf( "}" ) );

	if( typeof oldEvent == "function" )
	{
		oldInnerSource = oldEvent.toString()
		oldInnerSource = oldInnerSource.substring( oldInnerSource.indexOf( "{" ) + 1, oldInnerSource.lastIndexOf( "}" ) );
	}
	eval( "obj." + eventType + " = function(){ " + oldInnerSource + newInnerSource + " }" );
}

/* suckerfish focus on textboxes */
sfFocusInput = function( elements )
{
	for( var i=0; i < elements.length; i++ )
	{
		if( elements[i].tagName == "INPUT" && ( elements[i].type != "text" && elements[i].type != "password" ) )
		{
			// Don't apply this to other input types (buttons, radios, etc.)
			continue;
		}

		stackEvent( elements[i], "onfocus", function()
			{
				this.value += "1";
			}
		);
		stackEvent( elements[i], "onfocus", function()
			{
				this.value += "2";
			}
		);
		stackEvent( elements[i], "onfocus", function()
			{
				this.value += "3";
			}
		);
	}
}

/* suckerfish method can be found at http://www.htmldog.com/articles/suckerfish/ */
suckerfish(sfFocusInput, "INPUT");
My Girlish Figure

I’m sure you picked up on the hints last week. I’ve signed up for WeightWatchers.

I have a few friends who’ve had great success with WeightWatchers’ online point system. One young man in our youth group lost 70 pounds last year. It’s easy, inexpensive, and you don’t have to buy any special foods, just be more conscious of the things you already eat.

Ultimately it came down the the fact that most of my “XL” shirts have become uncomfortably snug and my clever tactic of doing nothing about it was not working out for me.

Today marks the first day of week two. I am allowed 37* points a day, with 35 points of overflow to use throughout the week. I did really well last week, averaging about 30 points a day, until the weekend. My in-laws were in town and we spent the whole weekend together shopping and eating and site seeing and eating and eating and eating. I honestly surprised myself when I tallied up the points for Saturday and topped 50 points. That’s much closer to what I was eating on a regular basis before I started. Not every day, but at least a couple of days a week.

I weighed in this morning at 1 pound less than last Monday. I could probably lose one pound by getting a hair cut, so I’m calling the first week a wash. But it was a learning experience and that’s better than nothing.

WBQotW #111

Seems like nutrition is going to be a common topic for the foreseeable future here at SYNS.

Why would someone order a double cheeseburgers, large fries, and a diet coke?

Sneaky Sneaky Calories

I wanted to check on the nutritional value of a salad I had for lunch the other day. So I logged on to Wendy’s web site. I always look for the PDF document version of the nutrition info, rather than try to use some fancy fangled web form to find what I’m looking for. Some places will trick you with those web forms and not tell you the whole story.
Once, I’ve found the PDF, my eyes scan down the page, looking for “Mandarin Chicken Salad”. Ah. Here it is.

170 calories and 2.5 grams of fat. Say, that seems healthy enough. It’s only 3 points on WeightWatchers*. Sweet!

But wait. Something is amiss. These numbers seem way too low. Could 170 calories include everything that was handed to me through the red head’s drive-thru window? NO! Strangely enough, they separate out the various other ingredients that will balloon your back side.

What?! But that’s … *seven… three… carry the two…* That’s 540 calories!! Wendy! You vixen! You lied to me. But why?! This salad is not 3 WeightWatcher points*. It’s 12!! Gah! Now I’m going to have to eat half a lettuce leaf for dinner! Curse your crimson braids, Wendy!

Okay, for the purpose of full disclosure, I’m being a little sensational here. I didn’t actually have the Mandarin Chicken Salad. I had the Chicken BLT Salad which is listed the same way, but the difference is not as dramatic. And if you do use Wendy’s fancy fangled web form, it does lump all the ingredients together and shows 520 calories (closer, I guess). But still it took me a bit to catch on and realize that that stupid Honey Mustard Dressing had almost as much calories and even more fat than the salad and it’s on-board chicken. So I’m still mad about it.

A big ol’ boy like me is allowed around 37* points a day to stay svelte and sexy. I’ll write soon about why WeightWatcher points matter to me.

Georgia Man Gets Hot Pocket from iPod

Don’t ya just love HotPockets? Those tasty little pastries, hard as granite on the outside and hot enough to melt your teeth on the inside? MmMmm.

This story is not about that kind of HotPocket. It’s about an actual hot pocket. Flaming hot.

According to this story from an Atlanta TV station, Danny Williams’ two-year-old iPod Nano could easily have got him arrested under suspicion of terrorism. Danny works in an airport and while at work, the Nano in his pocket burst into flames. Thankfully, the TSA didn’t notice his smokin’ hot pants and he had a brochure in his pocket that protected him from the flames.

Apple has promised to replace Williams’ iPod if he sends it in, presumable so they can study it, but when the TV station tried to contact them, Apple wouldn’t talk.

Media trying to hush holiday story?

(Warning: This post got way too long because it really encompasses two separate issues: Deceptive media practices and ignorant, psuedo-religious ideology. Please read this with that in mind.)

I got an email from the AFA today asking me to buy and wear a button in support of traditional Christmas. Reason for the season kind of stuff. Not unusual. But the headline was about a school near Chicago that had “banned Christmas”. That’s a pretty bold statement. I appreciate the AFA but I’m keenly aware that they can get a little carried away in sensationalism, so I always do some background checking on their stories.

Initially, I found a bunch of blogs decrying the Oak Lawn school board for attacking tradition in favor of political correctness. The usual rancor you would expect. But all the rants linked to the same story from Chicago’s CBS affiliate. I read the story and even watched the news video clip and it seemed to me that it was all being over-blown. (Also something I’ve come to expect.)

This story and video report on a school board meeting at which a bunch of white folks say a lot of pretty stupid stuff* and the Muslim mother at the center of the controversy comes off as the most logical and level headed person at the meeting. So I begin to think, “Well, nobody is ‘banning Christmas’ here. It’s just a little Muslim mom asking that her kids be allowed to fast according to their religion. That’s nothing to get all mad about.”

But then, I found an older video which made much more sense. The blog links I had followed were referring to this video of the previous story in which the school did explicitly ban the celebration of “Halloween”, “Christmas”, and any other seasonal religious holiday. It was a knee jerk reaction to the display (and subsequent removal) of some Ramadan decorations. Instead, the school opted for sterile, politically correct “Fall” and “Winter” celebrations.

Now, normally, I would be upset about the religious ban, but the school board made the right decision in the end (according to the later story, allowing Christians and Muslims to celebrate). What I find really disturbing is that the CBS web site clearly replaced the first story with the second, using the same URL to cover up the upsetting story with the soothing one, hiding the story about religion being banned from school with the story in which the school’s “tolerant” final decision is made and the “good, white, Christian folk” are displayed using very poor logic* to condemn the school’s earlier decision.

Is anyone else bothered by the link switching going on here? If I linked to a story about religious intolerance and that link was later changed to a story about religious tolerance, I’d look like an idiot. Perhaps that was the TV station’s intention. Either way, it stinks.


* Now, I want to expand on this “stupid stuff”:

I’m very conservative and very nationalistic. I’m also a very vocal Christan and I tend to stand on the side of keeping Christmas Christian and public. But I really get aggravated when people use poor logic and bigotry to support their point. It makes the rest of us look bad. Here are some quotes from the parents at the school board meeting:

“If Muslims want the school holidays, menus and school traditions to become tailored to their needs or beliefs, then they should go to private school next to their mosque.”

So, tell me, sir: Why aren’t your kids in a Christian school? If your kids are in a public school, you should expect them to be exposed to multiple cultures. If you expect your public school to celebrate Christmas, you should not be surprised when they also learn about Ramadan. Your double standard in unconstitutional.

“They’re trying to take away holidays and stuff for the kids,” said resident Gene Boerema, dressed in a Santa Claus costume.

Wow. You really came out to support your religious beliefs, huh? Cuz we all know that Santa delivered baby Jesus and put him in a brightly wrapped box under a decorated pine tree. A Santa costume at a school board meeting? I think someone just wanted to make the news. People who turn serious issues into a circus really irritate me.

“We’re letting you come here, were honoring you, don’t dishonor us.”

Seriously?! You “let” people of another religion “come here”? What “here” are you talking about? “Your” school? “Your” neighborhood? “Your” country? You may need to brush up on your American history a little bit. “Here” isn’t “ours”. “Here” actually belonged to “them derned Injuns” first and the multitude of people to came “here” from somewhere else, I assure you, were not all Christians. It is very ignorant of you to assume a religion that is foreign to you is foreign to America. Now, if you’re talking about illegal immigrants using tax-payer funded schools and health care, that’s one thing. If you’re talking about Islam as something inherently foreign, you’re only exposing your own bigotry and stupidity.

Ok. I’ve ranted far too long. I’m going to have to let this one be and go take some deep breaths.

You know you’re from Amarillo if…

Yep, I got one of those emails. The one with the two pages of forward headers at the top and five ‘>’ at the front of each line. But this one had some gems in it. And since I am from Amarillo, I have to share a few lines

You know you are from Amarillo, Texas if…

  • Friday night high school football is a sell-out.
  • You laugh at out-of-towners who actually attempt to eat the free 72 oz. steak.
  • You think that trees naturally grow leaning to the north east.
  • You know where The Canyon is.
  • You’ve never seen smog, but you’ve been through a dust storm.
  • You know that the Cadillac Ranch isn’t a real ranch.
  • 40 mph wind is, ‘a little breezy.’
  • You’ve had to pull over and remove tumbleweeds from the grill of your car.
  • You’ve seen rain, hail, sleet, and snow all in the same storm.
  • You’ve seen tires on the roofs of trailer houses and you know they’re there to hold the roof on.
  • You know how to pronounce Dumas.
  • You accept the fact that the most prominent vehicles on the streets are pickup trucks, SUVs, tractors, then cars (in that order).
  • You can get a sunburn one weekend and the next weekend it snows!
  • Some mountain weirdo or hill country hippie has called you a ‘flat lander‘ and you were proud of it.
  • You tell people you live in a red brick house with a tan shingled roof and and a tiny little tree in the front yard, and then realize that describes every house within a 5-mile radius.
Wha.. Wednesday? Already?!

It’s Wednesday, the unofficial cut-off time for posting a new white board quip. You’ll have to wait for next Monday. Sorry about that.

I’m not sure exactly what’s going on. I swear today feels like Monday. *Peeks around the corner to see if Rod Sterling is doing an introductory monolog about me.*

© Copyright 2004-2005, Light-Spark Design
Powered By WordPress