Archive for January, 2008

More JavaScript behavior differences between Firefox and IE

In addition to my previous post about JavaScript behavior difference between Firefox and IE, here are two more:

1. If JavaScript attempts to access HTML before IE finished loading it, you might get “Operation aborted” error. Firefox appears to be very flexible on this and will execute such JavaScript without error. To work around this error, delay the access until onDOMReady or window.onload event fired. Here is another blog entry on this topic.

2. You can load the following JavaScript in Firefox if myObjectType constructor exists and works.

a = new myObjectType();

However IE does not like the code if variable a is not declared earlier and will not stop complaining until you add the declaration. Here is one way to do it.

var a;
a = new myObjectType();

This is one difference that I actually prefer IE’s behavior, so I know a bad syntax exists in the code and fix it before it leads to other unintended consequences.

Leave a Comment

Very simple JavaScript tuning

I noticed that a piece of JavaScript code takes a long time to execute. So, i created a simple time stamp code to help me better understand how much time the code takes to execute:

var startTime = new Date();
// do some processing
var endTime = new Date();
var timeDiff = endTime.getTime() - startTime.getTime();

After some tweaking, I found out that if I create HTML on a temporary variable and do a single innerHTML call at the end is significantly faster than calling innerHTML on every little piece of HTML code that I want to add. This simple change improved my code from 1800 ms to 25 ms, what a big difference!

Leave a Comment

split files

I needed to split a file into a smaller size and refreshed my memory on using split and cat commands. Here are my notes:

split --suffix-length=1 --bytes=1024m source_file prefix.

After the split command run, you should get files with the following names:prefix.a, prefix.b, prefix.c, etc.

To recreate the original file, just run the following command:

cat prefix.a prefix.b prefix.c > original_file

Leave a Comment

Minify JavaScript

JSMin can reduce the footprint of JavaScript code by removing unnecessary characters. I used the Java implementation and it does work in most cases.

Leave a Comment

A JavaScript behavior difference between Firefox and IE

I just found out the following difference of behavior between Firefox and IE. Here is the input JavaScript:

if(a = b)
    // do something

Firefox will second guess what the programmer was really trying to do and treat the code in the following way:

if(a == b)
   // do something

IE will not attempt to translate the code and comparing the two variables. Instead, it will re-assign the variable a as the code literally stated. This is just one of many examples that shows Firefox is a lot more forgiving and attempts to render or execute incorrectly written code. IE shows strict behavior and will not work unless the code is syntactically correct.

Comments (12)

Use JavaScript to display HTML as text

I found a need to display HTML as text. The recommended way to do this is to escape the HTML markup so browsers will correctly display the code as text. I found a sample code here.

Leave a Comment