Friday, April 8, 2011

jQuery nth-child counts wrong in IE ??

I came across an interesting problem today, we are using a nth-child selector in jQuery.

$(".tag_even tr:nth-child(2n)").addClass('even');

Somehow a person code something wrong in the table, he inserted a <div> as one of the <tr>.

Because the selector in jQuery target at "tr", so the div should be ignored.  It make senses, right?

This assumption is true for FF / Chrome and Safari, but IE still count the "div" in? What the hack??

It is because IE wrap the table each children elements with a "nodeIndex", and jQuery when in IE, it uses "nodeIndex" for counting the nodes, no matter it is a "tr" or not.  The below is how jQuery implement the nth-child selector, and please look at the "bold" part that it is using "nodeIndex".


                CHILD: function(elem, match){ 
                // ... 
                        case 'nth': 
                                        var first = match[2], last = match[3]; 
                                        if ( first == 1 && last == 0 ) { 
                                                return true; 
                                        } 
                                        var doneName = match[0], 
                                                parent = elem.parentNode; 
                                        if ( parent && (parent.sizcache !== doneName || ! 
elem.nodeIndex) ) { 
                                                var count = 0; 
                                                for ( node = parent.firstChild; node; node = node.nextSibling ) { 
                                                        if ( node.nodeType === 1 ) { 
                                                                node.nodeIndex = ++count; 
                                                        } 
                                                } 
                                                parent.sizcache = doneName; 
                                        } 
                                        var diff = elem.nodeIndex - last; 
                                        if ( first == 0 ) { 
                                                return diff == 0; 
                                        } else { 
                                                return ( diff % first == 0 && diff / first >= 0 ); 
                                        } 
                        } 
                // ... 
                }, 


Thursday, April 7, 2011

Another Post about eval() is Evil

Not only eval() is evil, the following functions are something you need to be careful when using it (Basically those four functions are coming from the same family).

eval() // the famous "evil"
Function()
setTimeout()
setInterval()

Please do not use those function to evaluate string. Because everytime when you do that, the javascript needs to evaluate the string, parse it, and then execute using the compiler. And this will cause you a so called "double evaluation penalty" which will slow down your script.(well, of course, you don't need to care if you are just a small website).  And also, using those functions give you access to compiler which could pose security issues.

In short
So stop using eval() and Function() if possible. Well, there is no problem to use setTimeout() and setInterval(), the only catch is, please avoid passing string to these 2 functions.

Image not showing in IE

Symptoms: My JPEG image shows in FF/Chrome, but it doesn't show in IE??

If you have that issue, there is a simple fix. Try to resave your image using photoshop or other photo editing softwares in RGB color mode.

The reason is sometimes your image is in CYMK color code, which won't be understood by IE. Because IE so far only understand RGB, and CYMK is a print format, not really for web.

For people who are using photoshop, when you save your image, use the "save for web" options.

Wednesday, April 6, 2011

From Failure to Success Culture

We are in a culture that encourage success and high performance, no people really talks about failure. Because people tends to make the term "failure" same as "nothing good in it" or "shame". But how many "winners" out there have never once been a "loser" at certain time or how many of them have never made a mistake that brought them into all times low??

Most of the people must have experienced failure at a particular time, but it is not the end of the story. Every time you experienced a low is also a time that you can learn from it, which is the path to your future success.

But somehow, in our culture, we are made to deny our failure or to blame others for the failure. And thus we are not at all benefited by learning from those valuable lessons, and that is the worst of all.

I'm not encouraging failure, and I'm not telling you to pursuit for failure; But what I'm telling you is, life is a lesson, and you are always in learning process until you finish the course.

A wisdom quote

Please do not hide when something goes wrong, or pretend things will go right eventually by itself. 

Tuesday, April 5, 2011

A New Word - Greweet

Wow, I just invented a new word today - Greweet ( Gre - weet ).

It's the combination of "Great" and "Sweet".

It is used for expressing your appreciation of somebody's "exceptional" performance or action.

Usage: Well done!! You are so Greweet!

Updated:- I have submitted the word to urban dictionary, and it is published!!
http://www.urbandictionary.com/define.php?term=Greweet


Monday, March 28, 2011

Yet Another Optimizing your Javascript

If you are javascript guru or you have been using javascript for a while, this post could be quite basic for you. But it could be a good review if you will.

First, let's talk about some techniques that without you writing any code.

1) Less is More
The more javascript file you have, the longer the download time for your users. The same goes for file size. So it is not good idea to include things that you don't really need. If you really need to include multiple javascript file, try to combine them together; or you can use third party tool to "bundle" it ( just another word for combine your file ). There are also tools that can minimize the file size for your javascript. Try googling words like "javascript minify" and give it a try. Just through minification and bundling, you should see a "significant" amount of performance increase for your site.

2) Where to place your "script" tag
It is usually preferable to place your "script" tag before the closing "body" tag. The reason is because javascript during its download will block the flow of your page. If you have a big javascript file and you place it at the "head" section, your page may show blank screen until your javascript file is downloaded. Unless your javascript code need to be run before the page loads, you should place them before the closing body tag . Btw, you don't really need to put "type" and "language" attributes in your script, because those are all implied.

3) Dynamic download or Loading on Demand
Another way of including javascript is using dymamic include (non-blocking ) or using a method call Lazy Loading (which is Loading on Demand ). This way, javascript only get called when it is needed. Try googling the terms "requireJS" and this is one of the tools doing this stuff. If you want to, you can roll out your own, it's just using javascript to create script tag on the fly.

4) Avoid using global variable
Make sure you use "var" when you declare a variable, or else it will become a global variable. And it is slow. The reason is because the interpreter needs to search for where your variable is declared in the code if it is global. Plus, a global variable have other potential problems besides speed, for example, namespace collision.

5) Cache your variable as needed
For example, if you need to access an array length in a loop, instead of calling "arrayX.length" everytime, you can cache that value in a variable, so there is no need to search for that value in every loop. In the same token, if you have a value or object needs to be accessed for multiple time, and if that value / object won't be changed, it is a good candidate to be cached.

6) Understand Delegation
For example, you have an unordered list of 100 links. Instead of binding each links a "click" event, you can just bind the click event to its parent, which is only 1 item. This way, it saves you coding time, and save the parse to search the DOM for 100 times. And that is what called Delegation.

7) Code Reusability
Understand how to reuse your code will definitely makes your code cleaner and you are writing less code. For starter, let's give you some keywords in this post. I will write another post on those later.

a) Inheritance - either Classical or Prototypal.
b) Borrowing other object method ( call, apply, bind )




Monday, February 28, 2011

The secret Ingredients to Profitable Internet Business

1) Focus on partners, not competitors.

2) Think locally.

3) Target each customer in multiple segments.

4) Transforming pricing into a dynamic conversation.

5) Make use of the community (platform) to improve customer service.

6) Humanize your company "virtually"

7) Build trust and credential for your products / services.