Mar 06 2009

parseInt(): difference between Javascript and Actionscript

Category: actionscript,javascriptDavide Zanotti @ 8:27 am

Today is the day of the little big discoveries. I realized that the parseInt() function, which objective is the same in both languages (Actionscript and Javascript), is a bit different between them. In Actionscript the function is based on base 10 numeration, in Javascript instead the funny thing is that it try to guess the radix by analyzing the string received as first argument, so if you want to be sure of the result returned you have to explicitly pass the radix in the second argument.

Example:

var myString = "000123";
alert(parseInt(myString));

The code above will alert 83 instead of 123 (because the default base guessed by Javascript isn’t 10 like in Actionscript), to get 123 the code must be:

var myString = "000123";
alert(parseInt(myString, 10));

Tags: ,


Dec 12 2008

Using getAttribute() to retrieve the label’s "for" attribute returns NULL on internet explorer!

Category: browsers,javascriptDavide Zanotti @ 3:40 pm

Damned Internet Explorer!!! I’ve just faced another browser issue by using the javascript function getAttribute() in order to retrieve the “for” value of a form’s label, ie:

myLabel.getAttribute("for");

(where “myLabel” is a reference to a <label> node)
The function always returns NULL, even if the “for” attribute is manually specified into HTML code, ie:

<label for="my_field_id">my label</label>

Fortunately, after a brief search on Google, I’ve found a post on quirksmode.org, which contains a comment by Tino Zijdel, that show an easy and crossbrowser solution to get the “for” attribute (that can be used both to set a value or to get it):

myLabel.htmlFor

Thank you Tino!

In my search I’d even discovered that on Internet Explorer getAttribute() is different from other browsers, because it offers an extra argument called “iFlags” that is a number that can be 0, 1 or 2 (genial!) and means (I’m reporting the official microsoft documentation):

0 -> Default. Performs a property search that is not case-sensitive, and returns an interpolated value if the property is found
1 -> Performs a case-sensitive property search. To find a match, the uppercase and lowercase letters in AttributeName must exactly match those in the attribute name. If the iFlags parameter for getAttribute is set to 1 and this option is set to 0 (default), the specified property name might not be found.
2 -> Returns the value exactly as it was set in script or in the source document

It would be a better world without Internet Explorer!

Tags: , , , , , ,


Dec 09 2008

Javascript string concatenation performance on modern browsers: array.join("") is now slower!

Category: javascriptDavide Zanotti @ 3:52 pm

Well… as many Javascript developers know, a common practice to handle big string concatenation, is to implement a sort of  “stringBuffer” by using an array as data container and then convert it to a string by using the method join(). Ie:

var buffer = [];
var target = document.getElementById("box");

for (var i=0; i<10000; i++) {
    buffer .push(['<p id="node_', i, '" id="node_', i, '">Content ', i, '</p>'].join(""));
}

target.innerHTML = buffer.join("");

This is theoretically the fastest way to manage huge strings and to speed up the building of dynamic content.

Continue reading “Javascript string concatenation performance on modern browsers: array.join("") is now slower!”

Tags: , , , , , , , , , , ,


Nov 26 2008

Playing with Google maps API – part three: Geocoding and custom overlay

Category: javascriptDavide Zanotti @ 1:45 pm

The main feature provided by the Google maps API, is the ability to convert an human readable location, such “Milan, Italy” to a geographic location represented by latitude and longitude, this process is called geocoding. The GClientGeocoder is the class which provide this service, through the method getLatLng() which accepts two arguments: a string representing an address and a callback function that will be called once the coordinates are retrieved. If the address will be successful located, the callback function will be invoked by passing a GLatLng object as argument, otherwise the function will receive a null. GLatLng is an object which represents a geographic location (latitude and longitude).

To request a geolocation for a given address we use a similar code:

Continue reading “Playing with Google maps API – part three: Geocoding and custom overlay”

Tags: , , , , , , , , , , ,


Nov 17 2008

Playing with Google maps API – part two: Create custom controls

Category: javascriptDavide Zanotti @ 2:11 pm

Today we are going to see how to realize custom controls to add to our Google maps powered map.

The only concept that should be clear, in order to achive the goal, is what prototype is and why we must use it.

Javascript is not a really object oriented language, so we can’t really talk about classes and subclassing (using extends like in Java or Actionscript), but however we can create functions that act as constructor for new js objects, and we can inherit properties from an object to another by using the prototyping technique.

Continue reading “Playing with Google maps API – part two: Create custom controls”

Tags: , , , , , , , ,


« Previous PageNext Page »