Storing JavaScript objects in html5 localStorage

There is a rising new age of HTML5, which comes with very nice features as video tag and localStorage. What localStorage is? In fact we have been waiting for long time to have something that give us the power of storing more and more on the client side. That’s because the net is becoming larger and therefore heavy.

Imagine if you have the possibility to store large JSON returned from an AJAX call on the client. That’s now reality with the localStorage object in HTML5 and it can be accessed with something like this code:

localStorage.setItem(key, value);
alert(localStorage.getItem(key));

Can we store entire objects?

Yes there is a way extending a bit the logic of the code above. The localStorage in fact stores only strings, but what about arrays and objects. You can additionally use JSON global objects and its two methods – stringify and parse:

var a = JSON.stringify({name:'obj'});
alert(JSON.parse(a));

Do browsers support all that?

Of course … not! localStorage is supported by Firefox 3.5+, Chrome 2+, Safari 4+ and MSIE 8+. The solution is not to rely on browser version, but to check for object existence, and than to combine both techniques:

localStorage.setItem('a', JSON.stringify({ name : 'obj' }));
alert(JSON.parse(localStorage.getItem('a')));
Posted in javascript, micro tutorial | Tagged , , , , , , , , , , | 3 Comments

XAMPP and virtual hosts on Mac

Do virtual hosts work on your XAMPP?

In my case they didn’t. What I mean when tell you this is that every simple technique I tried to setup a simple virtual hosts was completely non sense. They simply didn’t work out.

Did you managed to setup everything?

It appears so! Everything was OK. The /etc/hosts file, the httpd-vhosts.conf and the httpd.conf.

Something really important was missing …

Simply to uncomment the following line:

Include /Applications/xampp/etc/extra/httpd-vhosts.conf

Now everything’s working like a charm! After fixing this I start to realize that this problem is not Mac specific, but however …

Posted in web development | Tagged , , , | Leave a comment

Get the browser locale and language from Zend_Locale

In a short snipped I’d like to mention the method of use in Zend_Locale:

$locale = new Zend_Locale();
$browserLocale = $locale->getBrowser();

it’s quite useful! Sometimes you don’t need to redirect user only by IP or domain name. Think about browser’s language!

Posted in zend framework | Tagged , , , | Leave a comment

jQuery 1.4.2 comes with performance improvements!

The new version of jQuery – 1.4.2 comes with some very good news. Check out the performance improvements here.

It’s amazing how radical can be an improvement release on this stage!

Posted in javascript, web development | Tagged , , , | Leave a comment

jQuery tips – storing data

Storing data?

I was reading an article where the author says it is not a good practice to store data in the DOM. Indeed this is a bad practice and you should avoid it. After that he gives an example with using the DOM with the ALT attribute:

$('selector').attr('alt', 'data being stored');
// later the data can be retrieved using:
$('selector').attr('alt');

and after that the more clean:

$('selector').data('paramtername', 'data being stored');
// then later getting the data with
$('selector').data('paramtername');

OK, is more clean. But however it is bad practice to use such a “heavy” construction to store local data. It is obvious you can prefer to store it in a local variable. Thus you avoid the extra performance.

Posted in javascript, web development | Tagged , , , , , , , | Leave a comment