Lazycoder

27May/108

Quick jQuery hack to fix position:fixed toolbars in iPhone/iPad/iPod Touch

This is just a quick fix if your postion:fixed elements end up in weird places when your site is viewed on the iPhone, iPad, or iPod Touch.

Say you have a div with an id of "#footer" that you want to stay at the bottom of the page. If you set it's position to "fixed" and set the bottom to "0px". When viewed on an iPad, iPhone or iPod Touch, the footer may end up in the middle of your content if you have a long page.

CODE:
  1. //stick the footer at the bottom of the page if we're on an iPad/iPhone due to viewport/page bugs in mobile webkit
  2. if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
  3. {
  4.      $("#footer").css("position", "static");
  5. };

17Mar/10Off

Announcing the Border Radius Support plugin

I’ve developed a jQuery plugin that will detect the specific css styles for setting the border radius of elements that the browser supports. The plugin will return an object containing three boolean properties: moz, webkit, and css3. Each property corresponds to a corresponding css style property

moz – MozBorderRadius

webkit – webkitBorderRadius

css3 – BorderRadius

JAVASCRIPT:
  1. var supported = $(“#foo”).borderRadiusSupport();
  2. console.log(supported.moz); //true for Mozilla
  3. console.log(supported.webkit); //true for Safari, Webkit, and Chrome
  4. console.log(supported.css3); //true for Chrome

Support for more browsers may be added in the future.

Link to the repository on GitHub

10Dec/097

Benchmarking a simple DOM based cloning template

Sara Chipps recently posted a simple DOM based clone template method she uses in one of her apps. "Easy HTML Templating with JQuery"

My template looks like this:

CODE:
  1. <script id="ItemTemplate" type="text/html"
  2.         <li class="item" value="|rowNumber|">
  3.               <input type=”text” id=”input|rowNumber|” />
  4.         </li>
  5.     </script>


Now within my code I need to put a place holder where I want my HTML to go. I have my unordered list called url_list.

CODE:
  1. <ul id="url_list"></ul>

Now, you see that most of my items look like this “|rowNumber|” I have a variable in my code called nextUniqueItemID (I believe in extremely descriptive variable names). Here is my “addItem” function.

JAVASCRIPT:
  1. function addItem() {
  2.         var list = $('#url_list'),
  3.                       items = list.find('li');
  4.         list.append($('#ItemTemplate’)
  5.                                     .html().replace(/\|rowNumber\|/gi, nextUniqueItemID++))
  6.     }

The use of global variables aside (cough,cough),I looked at it and, having used something like this myself, thought that it would work find for data sets containing a very small number of items. The problem is these kinds of clone based templates are VERY slow compared to the templating engines that are available for various JavaScript libraries.

I happened to read a post by Brian Landau called "Benchmarking Javascript Templating Libraries" this morning and wondered just HOW MUCH slower is the naive template method than a good template library?

I grabbed the benchmarking code and modified it to run the new clone based template method.

JAVASCRIPT:
  1. var nextUniqueItemID = 0;
  2.     function addItem() {
  3.         var list = $('#url_list'),
  4.         items = list.find('li');
  5.         list.append($('#ItemTemplate').html().replace(/\|rowNumber\|/gi, nextUniqueItemID++));
  6.     };
  7.    
  8.     $(document).ready(function(){
  9.         var output = $('#output');
  10.         $.benchmarks = {};
  11.    
  12.       $.benchmarks.test_simple = function(){
  13.         addItem();
  14.       };
  15.      
  16.       $.benchmarks.loop_test = function(){
  17.         for (var i=0; i <5; i++){
  18.           addItem();
  19.         }
  20.       };
  21.  
  22.       // use these lines to run the benchmark tests in your browsers JS console
  23.       // $.benchmark(1000, '#simple_test', $.benchmarks.test_simple);
  24.       // $.benchmark(1000, '#loop_test', $.benchmarks.loop_test);
  25.     });

Since the template Sara provided contains an input tag you get a different benchmark if you run the simple_test and the loop_test separately after refreshing your browser. You can run the tests for yourself here, the loop test *may* cause your browser to give you a "script is running slow" message, hit continue as the loop will eventually end. You may also get different numbers if you run the tests in IE, Chrome, and Safari.

results: using FF 3.5.5
Simple Test: 1.71s
Loop test: 31.534s

When you consider that the slowest loop test using a template library was just around 4.5s, you get a better idea of just how slow this method is when you have an input in your template.

So that's fine, but it's known that dynamically adding text inputs is slow in just about every browser and the original tests don't use inputs at all, just divs. So let's modify the template and see what the results are.

CODE:
  1. <script id="ItemTemplate" type="text/html">
  2. <div class="test"><h2>This is a test of |name|</h2><p>The homepage is <a href="|url|">|url|</a>.</p><p>The sources is: |source|</p></div>
  3. </script>

I modified the addItem function to account for the new data. n.b. The data I'm using is static, if you wanted to use a data source you would just modify this method to take in your data parameters.

JAVASCRIPT:
  1. function addItem() {
  2.         var list = $('#url_list'),
  3.         items = list.find('li');
  4.         list.append($('#ItemTemplate').html()
  5.             .replace(/\|name\|/gi, "Clone template method")
  6.             .replace(/\|source\|/gi, "http://girldeveloper.com/waxing-dev/easy-html-templating-with-jquery/")
  7.             .replace(/\|url\|/gi, "http://girldeveloper.com/waxing-dev/easy-html-templating-with-jquery/"));
  8.     };

results using FF 3.5.5 - refresh between each test
simple test: 1.285s
loop test: 3.771

results using ff 3.5.5 with no refresh between tests
simple test: 1.434
loop test: 4.227

So that's looking a little bit better. Not too much slower than the template libraries.

So what do the template libraries give you? Well the replace method works find provides your data is escaped properly. But say instead of a url in the "source" replacement, you use a file path like "file:\\foodrive\source.txt". Well it still gets replaced, but the text looks like this "file:\foodrivesource.txt". So in addition to the replacement, you have to make sure your data is properly escaped. A lot of template libraries will do this for you. Also notice that the addItem method has to do a DOM lookup on every iteration of the loop to get the template. If you have a large DOM, this could impact the performance.

29Sep/093

A new JavaScript CDN from Microsoft

Microsoft announced a new Content Delivery Network for their ASP.NET AJAX JavaScript libraries and jQuery. This means that instead of hosting those libraries on your server, you can just link to the versions on Microsofts server. I made a simple page that takes a querystring parameter (q=) and uses the ASP.NET AJAX dynamic templates to bind search results from a call to the Bing API.

The money lines in the source are the following:

CODE:
  1. <script type="text/javascript"
  2. src="http://ajax.microsoft.com/ajax/beta/0909/MicrosoftAjax.debug.js"></script>
  3.  
  4. <script type="text/javascript"
  5. src="http://ajax.microsoft.com/ajax/beta/0909/MicrosoftAjaxTemplates.js"></script>

These two lines tell the browser to load the MS AJAX scripts from the CDN. There are some security concerns around the fact that the files are served from the microsoft.com domain. Both Google and Yahoo serve there files from a separate, non-cookied domain (googleapis.com and yahooapis.com respectively). Hopefully, these fears will be unfounded.

On a side note, it is surprisingly easy to use the Bing API if you are familiar with script tag injection. The easiest way is to put an empty script tag in your page.

CODE:
  1. <script id="jsonResults" type="text/javascript"></script>

And then just create your Bing URL and set the script elements src attribute to the URL you created.

CODE:
  1. function MakeSearchRequest(searchPhrase)
  2.             {
  3.                 var req = "http://api.bing.net/json.aspx?"
  4.                 + "AppId=" + YOUR API KEY GOES HERE
  5.                 + "&Query=" + searchPhrase
  6.                 + "&Sources=Web"
  7.                 + "&JsonType=callback"
  8.                 + "&JsonCallback=BuildResults";
  9.                
  10.                 $get("jsonResults").src = req;
  11.             };

Filed under: Javascript, Web, jQuery 3 Comments
13Aug/092

JavaScript: Not for the faint at heart?

JavaScript: A tool too sharp?

Script# (Script Sharp) – writing javascript in C#

Both Jimmy and roy have great posts discussing JavaScript. Roy is looking at it as a C# developer lured by the many, many articles about how jQuery is the only thing that makes JavaScript worth using and using Script# to abstract away some of the messiness and pain usually associated with writing JavaScript. Jimmy discusses the merits of JavaScript itself and how it has changed how he approaches writing C# code.

One thing I like to point to is a great quote I heard on Twitter

Java is to JavaScript as ham is to hamster

hamster2.jpg

JavaScript actually has more in common with Scheme or Lisp than it does Java or C#. I first realized this when I saw that Douglas Crockford had re-written all of the examples in The Little Schemer
in JavaScript. It's easy to miss that fact when you see all of the pseudo OOP noise like "var foo = new Foo();". But when you see how trvial it is to implement something like a map method in JavaScript, you realize how powerful the language can be. Most of the hatred for JavaScript comes from two things I've found:

  1. Broken DOM implementations - every browsers implementation of the DOM is broken in one respect or another.
  2. A misunderstanding of either scope or inheritance.

Roy has a great point about the lack of good tooling surrounding JavaScript. There are excellent libraries like jQuery and PrototypeJS, but the usual tool support, intellisense, re-factoring, profiling, is a little more difficult to come by. I'll address this in another post as I feel a lot of people are new to JavaScript and are struggling along with some substandard tools.

12May/099

Why these jQuery worst practices aren’t.

jQuery ... Worst Practices

In this post, Steve Wellens tries to make the case for two common patterns your run across when using jQuery as "worst practices". Practices that are either superfluous or harmful to your code.

1) Wiring up events using unobtrusive JavaScript.

Instead of wiring up your elements events using jQuery, instead you should set the OnClientClick property of the ASP.NET Web Control in question and call whatever JavaScript you want to handle the event.

Instead of this:

CODE:
  1. <script type="text/javascript">
  2.  
  3.         $(document).ready(function()
  4.         {
  5.             $("#Button1").click(function(event)
  6.             {
  7.                 alert("You Clicked Me!");
  8.             });
  9.         });
  10.  
  11.     </script>

Do This:

CODE:
  1. <asp:Button ID="Button1"
  2.                 runat="server"
  3.                 Text="MyButton"               
  4.                 OnClientClick="alert('You Clicked Me!');" />

His reasoning is as follows:

You might want to change event handlers dynamically depending on some condition in the page. If you need to assign a common event handler to several objects on a page, it makes sense to do it in one place. As a matter of fact, that's the beauty of jQuery. But for a single event on a single control….NO.

On can debate whether or not a worst practice can really be applied to a single, specific , one-time event or not. But there are other reasons why you might want to hook up events to a single element using jQuery. one being that you can't hook up multiple event handlers using OnClientClick or HTML's OnClick event, only a single event handler can be assigned. If you are using the observer pattern in your JavaScript, you may have multiple observers that want to subscribe to your buttons click event. But a really strong argument could be made for using the OnClientClick property simply due to ASP.NET's suck-ass way of handling client IDs.

2) Chaining can lead to unreadable code.

In a badly written example, he states that chaining calls to jQuery makes the code unreadable. I'd suggest re-formatting the code in this manner to make it legible and allow for a tighter minimization.

CODE:
  1. $("div.highlight")
  2.   .css("color", "yellow")
  3.   .css("background-color", "black")
  4.   .css("width", "50px");

Of course, you wouldn't write jQuery code like that and call the css method multiple times. Ideally, you would just set a class or at the very least pass in a set of properties in an object.

CODE:
  1. $("div.highlight").css({
  2.   color:"yellow",
  3.   background-color:"black",
  4.   width:"50px"
  5. });

3) Comment your code.

CODE:
  1. // get all the Divs with the highlight class and
  2. // format them and set their width
  3.  
  4. var Divs = $("div.highlight");
  5. Divs.css("color", "yellow");
  6. Divs.css("background-color", "black");
  7. Divs.css("width", "30px");

I like this explanation by Jeff Atwood best. If you need comments to explain WHAT your code is doing, you need to use better names and/or write better code. Comments are best when used sparingly and explain the WHY of the code. Why did you choose to use a particular algorithm