I was chatting with Jim Cowart (@ifandelse) on Skype today about jQuery. He pasted in a snippet of code and Skype translated part of it into one of it's emoticons. He almost immediately updated the chat message to fix the issue, but it got me starting to think... and that can be very dangerous.
I thought to myself that it would be fun to create a program that intentionally utilize Skype emoticons. The following screenshot is the result of the program I put together. It is a restaurant where you can listen, eat, and drink ;)
As you can tell, it is a silly little program. The code for the above screenshot can be found below. Some of the icons I used are secret emoticons that you can lookup.
If you'd like to come up with your own Skype-enabled JavaScript program, then I'd like to see it. Share it in the comments. Have fun!
Web Dev .NET
Front-end Web Dev Tips, Tricks, and Tools
Friday, January 27, 2012
Having Fun with JavaScript and Skype Emoticons
Wednesday, January 25, 2012
Find the jQuery Bug #2: Point of No Return
Introduction
In this open-ended series I'll be showcasing a snippet of buggy jQuery code that you might encounter, explain what the problem is, and then identify how you can easily resolve the issue.
The Desired Feature
We want to take the following HTML unordered list and build a JavaScript function that will determine if a specific value is present.
The Buggy Code
The following code snippets is our first attempt at solving the problem, but there is a subtle error. Do you see it?
You can view, run, and edit the above code sample from JSBin.
The results that we expected were
false, true, true, true, false, but instead the output in the console is false, false, false, false, false. The Underlying Problem
At the root of the problem is the special jQuery
.each() method we are using. The .each() method is a very handy way to iterate over the jQuery collection, however there is a special meaning to return false within a .each() method."We can stop the loop from within the callback function by returning false." --http://api.jquery.com/each
Upon further examination of the code it even makes further sense why it wouldn't work. Most of us are familiar that a
return statement exists the current function, but in this case we aren't invoking the function... jQuery is! So, jQuery has control over what happens when you exit your function prematurely and it recognizes return false to mean something special.A Solution
The solution to fix this problem is really simple and straightforward. All you really need to do is to introduce another variable
hasNumber. If the number was found, then set the hasNumber variable to true and then return false; to exit the .each() method.You can view, run, and edit the above code sample from JSBin.
If you test out the code again below you'll notice that now that we are getting the expected output of
false, true, true, true, false.An Alternate Solution
An alternate way to solve this problem would be to use another technique completely. In the following example we will use the
jQuery.grep method. The jQuery.grep method is defined as the following:"Finds the elements of an array which satisfy a filter function. The original array is not affected." --http://api.jquery.com/jquery.grep/
You can view, run, and edit the above code sample from JSBin.
Although the code looks shorter, it is less performant than the previous solution. Can you tell why? The code is not exiting once the number has been found, but instead goes through each array item and executes the callback function. We get the correct answer, but for a price.
Conclusion
The key concept to remember here is to be aware of the special mean of
return false; inside of a jQuery .each() method to exit the loop. Even if you have never made this mistake before, maybe it has opened up your eyes to the fact you can exit out of a .each() method!Until next time...
Thursday, January 19, 2012
Daily and Weekly Front-End Dev Resources
If you are anything like me, you love and thrive on what is new in the front-end world. What is the latest in HTML5 buzz, what is the newest library that will solve all my needs, what is that new jQuery plugin that will make my client drool, and things of the like.
I used to scour the internet for things like that myself and share them on Twitter and elsewhere, but I've recently changed focus so that I can blog more frequently. My desire for tech news has not diminished in the least, I am still a junkie for new articles and libraries.
So, what am I to do? Well, that is easy. There are many fine resources that have been around for years and there are also some that have cropped up recently that should provide just the right amount of geek influx to quench my thirst and I hope yours as well.
Twitter
[Cross posted from Fresh Brewed Code.]
I used to scour the internet for things like that myself and share them on Twitter and elsewhere, but I've recently changed focus so that I can blog more frequently. My desire for tech news has not diminished in the least, I am still a junkie for new articles and libraries.
So, what am I to do? Well, that is easy. There are many fine resources that have been around for years and there are also some that have cropped up recently that should provide just the right amount of geek influx to quench my thirst and I hope yours as well.
Daily Technology News
- @JavaScriptDaily by Peter Cooper (@peterc)
- @JSCentral by Béla Varga (@netzzwerg) and Axel Rauschmayer (@rauschma)
- @JSGoodies by Peter van der Zee (@kuvos)
- @DailyJS by Alex Young (@alex_young)
- @FunctionSource by Dion Almaer (@dalmaer)
- @EchoJS by Frederic Cambus (@fcambus)
Blogs
- DailyJS: A JavaScript Blog by Alex Young (@alex_young)
- The Morning Brew: A Daily .NET Software Development Link Blog by Chris Alcock (@calcock)
- The Morning Dew: Your Source for .NET Development Resources by Alvin Ashcraft (@alvinashcraft)
- Function Source: Your Source for Developer News by Dion Almaer (@dalmaer)
- EchoJS: A HackerNews-like site dedicated to JavaScript and Front-end News by Frederic Cambus (@fcambus)
Weekly Technology News
Newsletters
- JavaScript Weekly by Peter Cooper (@peterc)
- HTML5 Weekly by Peter Cooper (@peterc)
- Web Design Weekly by Jake Bresnehan (@jake_bresnehan)
Podcasts
- The JavaScript Show by Peter Cooper (@peterc) and Jason Seifer (@jseifer)
- JavaScript Jabber: Your Prototype for Great Code hosted by AJ O’Neal (@coolaj86), Charles Max Wood (@cmaxw), Jamison Dance (@jergason), Peter Cooper (@peterc), and Yehuda Katz (@wycats)
[Cross posted from Fresh Brewed Code.]
Subscribe to:
Posts (Atom)
