22 November, 2007

Action-like URL

In VBScript, in an ASP page, I was looking for a way to declare a querystring parameter without giving it a specific value. In the requested page, I just wanted to check the presence of that particular parameter. My URL looks like this :

http://www.mesconges.fr/comprendre.asp?autoplay

The presence of the autoplay parameter indicate to comprendre.asp to run the first flash video once the page is loaded. Requesting comprendre.asp directly would result in no specific action after the load.

Believe me, it was not easy to test the difference between a querystring parameter that has no value and no parameter at all !
You have to play with :

isObject
isNull
isEmpty
<> ""
and so on...

The best way I found was : Not IsEmpty(request.querystring("autoplay"))

07 November, 2007

Restart Windows Server

Under windows server as under windows since 2000 version, if you don't find the Shutdown button in the start menu, don't worry, you can easily restart whith the MS-DOS console.
Click the start menu, choose "execute...", type "cmd" for opening the command line and type "shutdown -r".
The -r option tells the system to shutdown and then restart. Very usefull if your server is located somewhere in India and you don't want to pay fees for a employee to go pressing physically on the server start button !

18 April, 2007

Instructor for the NATIW

Next week, I am leading a workshop at Nomades Ateliers in Geneva.
The first day will be dedicated to AJAX and the second to the Dojo Toolkit.
Just after that, Sebastien Gruhier will lead a workshop about Ruby on Rails. Next week will 100% Web 2.0 !!
Fell free to read about the workshop and register.

14 February, 2007

workaround to make AJAX calls on Internet Explorer 6

I have found a nice alternative to the traditional try catch method to make AJAX calls work on every browser. Place the following code on top of your Javascript script.
if (!window.XMLHttpRequest) {
window.XMLHttpRequest = function() {
return new ActiveXObject('Microsoft.XMLHTTP');
}
}

This hack defines a function called XMLHttpRequest if XMLHttpRequest doesn't exist natively on the browser.
With this method, you can make AJAX calls with 'new XMLHttpRequest()' even on IE 6 or 5.5.
var ajax_call = new XMLHttpRequest();
...


UPDATE : Thanks to comments on Ajaxian, I added the "window." prefix to the declaration of the XMLHttpRequest function in order to work properly on IE 6 and not redefineing the XHR native object on IE 7.

29 December, 2006

workaround to make firebug console.log function bug free on IE

Firebug is very usefull for web development but its console only works great on Firefox for now.
If you want to keep console.log calls in your app event when your app is used on Firefox without the Firebug plug-in or on IE, you can add this line at the very first start of your JS code :

try { console.log('init console... done'); } catch(e) { console = { log: function() {} } }

This code tries to write some text in the Firebug console. If it fails (no Firebug plug in installed or under IE), it instantiate an object called 'console' that has a method called 'log' that does nothing !

05 December, 2006

BPM Counter

I have writtent a very, very simple tool for counting BPM of a song. All you have to do is to press the button when the bit happens. The value of the button indicates you the Bit Per Minute rate of you beeing a DJ !! Enjoy..

02 October, 2006

AJAX : le guide complet

A collegue and me have written a book about AJAX. It's integrally in french but the tutorials are in Javascript, so you might be able to understand them!

Description of the AJAX book
Buy our AJAX book at the best price!

09 August, 2006

Passing an object reference to a function

Here is another proof of Javascript spectacular flexibility. We want to draw 10 DIV in a page, representing 10 people. When we click a DIV, we want a message indicating which person was clicked. Given an object named person, we could hard code this doing :

div.innerHTML = person.firstName+' '+person.lastName;
div.onclick = function() {
alert(this.innerHTML+' was clicked');
}

But the object model would be useless in this case !
Our first try is to make a loop that creates 10 DIV and 10 objects and map each of it together. Let's look how it works :

<div id="myGroup" style="border: 1px dashed #000; padding: 5px; width: 300px;"></div>

And the script :

//A class Person
function Person( firstName, lastName ) {
this.firstName = firstName;
this.lastName = lastName;
}
//A function to be executed when onclick is fired
function clickFunction(person) {
alert(person.firstName+' '+person.lastName+' was clicked');
}
//We create 10 DIV representing 10 people
for (var i=0; i < 10; i++) {
//Create a DIV
var div = document.createElement('DIV');
//Create an object
var person = new Person('firstName'+i, 'lastName'+i);
//Fill in the DIV with the object infos
div.innerHTML = person.firstName+' '+person.lastName;
//Some styling
div.style.border = '1px solid #CCC';
div.style.margin = '5px';
div.style.cursor = 'pointer';
//Set the event on the DIV
div.onclick = function() {
//Use the object matched with the DIV in a function
clickFunction(person);
}
document.getElementById('myGroup').appendChild(div);
}

We have a DIV named myGroup which contains our 10 DIV
In each loop, we create a Person 'var person = ...' that is referred by the 'onclick' function of the DIV.
Try clicking on the people of MyGroup, you'll be quickly disappointed.
It refers to the last Person for any DIV we click on. WHY ?
Because the variable 'person' is re-written in each loop for a new Person. In the end, it lasts only 1 Person, person number 9!

One solution could be to push each Person in an Array and refer to the position of the Person in the 'onclick' function of the DIV. But Javascript has more to offer than this hack!
In Javascript, when a function creates a local variable that is refered by other functions, the variable is stored in memory until its last use.
We are going to exploit this capability like this :

//A class Person
function Person( firstName, lastName ) {
this.firstName = firstName;
this.lastName = lastName;
}
//A function to be executed when onclick is fired
function clickFunction(person) {
alert(person.firstName+' '+person.lastName+' was clicked');
}
//A function that is used to create a DIV matched with an object
function createDiv(index) {
//Create a DIV
var div = document.createElement('DIV');
//Create an object
var person = new Person('firstName'+index, 'lastName'+index);
//Fill in the DIV with the object infos
div.innerHTML = person.firstName+' '+person.lastName;
//Some styling
div.style.border = '1px solid #CCC';
div.style.margin = '5px';
div.style.cursor = 'pointer';
//Set the event on the DIV
div.onclick = function() {
//Use the object matched with the DIV in a function
clickFunction(person);
}
document.getElementById('myGroup').appendChild(div);
}
//We create 10 DIV representing 10 people
for (var i=0; i < 10; i++) {
createDiv(i);
}

What we do here is to create an intermediate function that aims to create local variables. Now the variable 'person' is stored at each loop, event if it has the same name! And each 'onclick' function refers to the right 'person'.

15 June, 2006

IE AJAX memory leak

We already know about IE memory leak pattern. It's a matter of garbage collection between JS objects and DOM objects.
Did you know about IE HttpRequest memory leak ??
It seems that there is the same symptom with HttpRequest object instanciation. In IE, that objetc is not JS native. It is an ActiveXObject. So that might be the cause.
I have recently faced important memory leaks in our new AJAX application. However, I had my own implementation working well for breaking DOM and JS cycling reference. The point was that my HttpRequest objets where refering to a higher level JS object that contained an array with references to these HttpRequest objets. So I had a cycling reference between standard JS object and ActiveXObject. I did the same breaking reference tool and it all disappeared!

24 April, 2006

AJAX training seminar

I am currently in Chicago for the next Pragmatic Studio! That training will help me preparing our own AJAX seminar taking place in Paris next month. If you are interested in such training, you can subscribe online.
The next step is to write a book talking about AJAX. It's an "AJAX from scratch" book written for personal users. We will shortly release the full table of content in order to get feedback before going further with the work.