02 October, 2009

LINQ to Javascript - make a Where

I find LINQ very cool for Data querying.

I tried to code a Where function for Javascript Arrays :


Array.prototype.where = function(condition) {
var list = [];
for (var i=0; i < this.length; i++) {
if ( condition.call(this[i]) ) {
list.push(this[i]);
}
}
if ( list.length == 1) {
return list[0];
}
else {
return list;
}
}


You havre to send a "condition" function in the where that will test each element and return an array contained elements that verify the condition.

The context of the condition is the element itself so you can easily access it with "this".

Suppose you have INPUT form elements in your document and you want to retrieve those which id contains "browserWidth".

Here is the code :


$$('input').where(
function() {
return (this.id.indexOf('serviceSelected') != -1);
});


If the return array contains a single element, it returns the element itself.

Access C#.NET Server controls by id on client side

In C#.NET, when you declare a Control runat="server", the id of the Control is not the same on the client side.

For example, a Control named "browserWidth" on the server side will have the following id on the client side : "ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_browserWidth".

Thus, you can't access it with a classic Javascript $("browserWidth"), and it is very difficult to guess what client id the Control will have.

My best hack for that is to use the "class" attribute, that can be multiple, and add a class which name is the same as the id of the Control, like this : class="browserWidth" or CssClass="browserWidth" if you work with an asp.net Control.

This way, I can access my client control with Javascript $$("*.browserWidth")[0].

The only problem comes with the asp:HiddenField Control that doesn't accept CssClass attribute.

On the client side, I have a tricks to access to it :

$$('input').where(function() { return (this.id.indexOf("browserWidth") != -1); })

The "where" fucntion is one of mine explain in the next Post.

This tricks works under two conditions :

- your Control has a unique ID
- the Server ID of the Control is contained in the client ID "ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_browserWidth"

If M$ changes the way client ID are made and decides to not add server control id at the end, that tricks would not work.

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'.