Cleanest way of executing javascript code on page load 2008-12-01

Everytime that I work on a new project, I'm always unsure about which way to execute javascript on page load. These are the different techniques :

Your old buddy body.onload()

The safest way to execute javascript when a page loads is to use the body.onLoad method.

<body onload="doSomething();"></body>

The document.ready function with jquery

This technique requires jquery and is similar to the body.onLoad technique (see above).


The problem is that I don't like that the javascript code is too close to the HTML code. There's really something that I don't like.

The #ID.ready

DO NOT USE! It doesn't work. At first, I tought that this technique worked but in fact, even if the element corresponding to the ID doesn't exists, the function is called. To understand it better, I'll give you an example. Suppose that I want to load a user list with ajax after the page load and that list correspond to ul#users, I'd write the following code

$("ul#users").ready(function() {

Unfortunately, it doesn't work.

The document.ready + #ID.each

After the failure of the #ID.ready technique (see above), I tried a similar thing that is working but ugly looking. Suppose I want to do the same as above, I'd write this :

$(document).ready(function() {
  $("ul#users").each(function() {

That way, it would execute the loadUsersWithAjax() method for each ul#users and because element IDs are unique to a page, the method would be called only once. It looks bad but it's the “prettiest” way I found of executing javascript code on page load.

Now, I want to know what would you suggest? Which technique is your?

JavascriptKata now ad free 2008-11-28

[Update 2009-09-20 : I will try to bring back ads. I removed them because I was frustrated, I bring them back as an experience.]

JavascriptKata was created in the blog bubble. In a time when all you had to do was to create a blog, put ads on it and you were rich. It never was true. The ads on this blog gave me about 100$ in a year so I finally felt discouraged and left it for dead. Nevertheless, this blog as a good share of traffic and more subscribers in the feed than any blog that I've owned/I own/will own. In fact, I call it a blog but it's not really a blog. I used wordpress and that's the main reason why I'm calling it a blog.

I tried several things to keep my interest in writing about javascript but they all failed because there was the devil saying “anyways, you don't make any money out of it”. But there was the angel saying “maybe you don't make money but you like javascript and should continue to write about it”. As usual, the devil won.

I won't try to make another revival of this blog but I'll try to write more about little things that happens between me and javascript, about the daily problems that I encounter. I'll write a lot about jquery too since for me, there can be no javascript without jquery.

Thanks to all of you!

Load a page with javascript 2008-02-12

I receive javascript-related questions in my inbox from time to time and here's the last one.

I am ok with HTML but awfully new to JavaScript and I know very little at this point. My question is for this web site I'm building. I need to know the exact JavaScript code and placement of the code in an HTML document for this goal. I would like this site to automatically load a certain page (1-31) depending on what day of the month it is (1-31). I don't need to worry about what month or year it is, just the date in the month. For instance when it is the eleventh day in the month, page11.html will automatically load when you click on to the site. I would like it to take the time(Date) from the client side not the server. This way no mater what time zone your in the right HTML page will pop up at midnight.
Most people can't give the exact javascript code placement of the code in an HTML document because it would always be buggy. They can give hints and/or snippets of code but rarely a complete working piece. Sorry…

Secondly, relying on javascript to do all this work would be a mistake because if javascript is disabled, the site will not work. Most users have javascript enabled but you should always think about web-crawlers (google, yahoo!, etc) that will try to index your page but will hit an empty page.

It's always hard to guess all the reasons why someone would want a different page everyday of the month and I don't know more about the project than what is written above. Considering this, maybe there are just parts of the page that are changing everyday and you could load them on the server-side thus having a single page loading including other pages depending on the day. The problem is that you would have to ask the timezone of the user and keep it in a cookie.

Just remember, javascript should not be a requirement to navigate in a site.

Finally, if I would really want to load a complete new page in javascript, I would do the following.

var dayOfTheMonth = (new Date()).getDate();
location.href="page" + dayOfTheMonth.toString() + ".html";


This is the way I would do it, do you have any other suggestions?

Javascript for PHP lovers 2008-01-30

I'm not a PHP guy and I like to use standards for the languages I'm using so this php.js thing is not for me at all. In fact, I really dislike PHP and though I use it often, there is no reason in the world I would want to use its syntax to write javascript.

Import the librairie (packed) and write javascript like you never wrote it before.

Example, you want to use the good ol' strrpos to find position of last occurrence of a char in a string (example from the site)

strrpos('Kevin van Zonneveld', 'e');
would return 16.

But what happens if a non-PHP guy as to modify your PHPed javascript?

How to have jQuery and prototype.js in the same project 2008-01-28

I'm a big fan of jQuery. This librarie is just the best and simplest one around. I really noticed it when I wanted to get rid of jQuery in TimmyOnTime and try to use prototype.js instead, just to be more “rails-oriented” (that's a pretty lame excuse don't you think?)

Why I didn't like prototype.js

There was not a lot of javascript written for the project so I thought that getting rid of jQuery and using prototype.js would be easy. It turned out to be hell on earth. Prototype.js made me feel like I was back in the 90s writing C++. A simple click event turns out to be a awful lot of ugly code with ugly function names.

Example, if I wanted to show the content of a DIV I clicked in prototype.js, I would use things like Event.observe, bindAsEventListener, a mix of native DOM element and prototype.js element, and worst of all, an unintelligible documentation.

jQuery as a complete different way of doing it in a single easy-to-understand line : $(“#adiv”).click(function() { alert($(this).text()); });. As simple as that!

The problem

I wanted to use some interface element in Rails that requires prototype.js so I had to have both librairies. The problem is that there's a conflict between them. Prototype.js doesn't seem to give a damn about it but jQuery is nicer with you. It offers a <a href=“” target=“blank”>noConflict method.

With Rails, prototype.js is the default librairie so to override this, you could do

<%= javascriptincludetag "prototype", "jquery" %>
<script>$j = jQuery.noConflict();</script>
This way, you will have to use $j instead of $ to call jQuery.$ would still call prototype.

Another thing you could do is to use jQuery on Rails and don't give a damn about scriptaculous. I personally prefer to use librairies that are fully tested instead of using a “by-pass” that could break my code. It's your choice…