stoimen.com/blog

web developing

JavaScript Comparision Snippet

== vs. ===

I know this is really for beginners, but have you ever asked yourself what are the differences between == and === when comparing in JavaScript, and when to use it? Here’s an example:

var a = false;
alert(a === 0); // prints false

That’s false. Even when both the zero and the variable are faulty the entire operation is false. Because the 0 is a number and the variable a is boolean, therefore their different types give us the answer. If there was a comparision only with == the result would be true:

var a = false;
alert(a == 0); // prints true

It’s good when you expect some variable to match certain type to compare with ===. So the correct code is:

var a = false;
alert(a === false); // prints true

Here the type of a compares with boolean. See the complete demo.

  • 1 Comment
  • Filed under: javascript, snippets
  • OpenLayers Can Be Faster!

    Put All The Markers at Once!

    The way OpenLayers puts markers on the stage is fine when they are not so much, but once you need more than 100, the library’s solution is not good. The question is is there a way to put all the markers faster than the built in method.

    Yes, There is a Way to Speed Up Markers Load

    I once wrote about how to speed up markers on OpenLayers, it is clear that my decision was to concatenate the DOM elements like a string after I’ve generated all the markers as markup with innerHTML as it’s faster than appendChild. The only question left is how to convert Longitude/Latitude pair in pixels.

    The Answer’s Given by OpenLayers

    There is a method called getLayerPxFromLonLat() which do the job. Let me show you a snippet:

    ll = map.getLayerPxFromLonLat(new OpenLayers.LonLat(lon,lat));
    console.log(ll);

    You can see what’s inside the ll object. You can now reference the pixel as ll.y and ll.x.

    Internet Mobile

    It isn’t strange that the internet is becoming more and more mobile. When it matters to news sites I think most of the mobile versions are better than the “desktop” versions of the site. Take a look at Huffingtonpost.com or LeMonde.fr.

    Mobile Ads

    The first thing that makes impression when comparing both mobile and desktop versions of a site is the layout. Of course the lack of ads on the mobile version, or if not the absolute lack at least the small number of appearing ads, seems to be quite pleasant.

    The thing is that as the web grow and as the web 2.0 is becoming a reality most of us will prefer to use it on a mobile device, just because it’s

    1. easier to use
    2. easier to find
    3. easier to follow

    Here’s a nice example of what mobile phones are now.

    Now lets take a look on how the mobile web will grow in the near future.

    That graphics describe quite well what will happen in the upcoming years. Is there now a question: should I make a mobile version of my site?

    PPK is right! Stop developing everything as a iPhone app. First that doesn’t work on any device except iPhone and it’s difficult to maintain. In reverse you should begin doing your mobile web site version. Hopefully with HTML5.

    Do as Twitter did … or you’d be looking at http://m.digg.com – that’s the right way.

  • 1 Comment
  • Filed under: web development
  • cssText

    You know you can manage to redraw an element with single browser reflow. Instead of using .style.property … you can simply add all CSS properties you’d like to change with simply appending to style.cssText property.

    var csstxt = $('#selector').css('cssText') + ';top:100;left:100;border:1px solid red;color:#f00;';
    $('#selector').css('cssText', csstxt);

    That code is a replacement for

    $('#selector').css({
       left : '100px',
       top : '100px',
       border : '1px solid red',
       color : '#f00'
    });

    enjoy!