Category Archives: micro tutorial

CSS Sprites – common mistakes

CSS Sprites

Although there are so many resources about CSS sprites and where they should be used I still think their usage is misunderstood.

The big problem is that many people use the sprite in a completely wrong way. When used properly the sprite can be really helpful, especially for a large scale site.

The mistake

When you read about sprites and you understand you must use only one image you start doing the big mistake. Let’s assume we’ve one image for the backgrounds of three div elements:

.a {
    background: url(sprite.png) 10px 10px no-repeat;
}
.b {
    background: url(sprite.png) 20px 20px no-repeat;
}
.c {
    background: url(sprite.png) 30px 30px no-repeat;
}
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>

Now even with this simple example there is one sprite image but the browser makes three requests for this image, which is really bad!

The solution

How about making it the right way with a simple change:

.sprite {
    background-image:url(sprite.png);
    background-repeat: no-repeat;
}
.a {
    background-position:0px 0px;
}
.b {
    background-position:10px 10px;
}
.c {
    background-position:20px 20px;
}

and a little bit different markup:

<div class="sprite a"></div>
<div class="sprite b"></div>
<div class="sprite c"></div>

and this is where the sprites are really useful. Now the browser makes only one request and your site is ready for the big world.

jQuery – stop an ajax call!

It’s a simple task with no simple question, although in jQuery, as I mentioned now, everything is very simple. Even this task.

In a short example we’ve a simple AJAX call with very large request.

xhr = $.ajax({
    url : 'www.example.com?some-large-call',
    success : function(responseText) {
        // some DOM manipulation
    }
});

But what if something changes the data we’d like to request with this call even before the response has come. For example I click on a category in the site but before all the data has come I’d prefer to see other category’s data. Continue reading jQuery – stop an ajax call! →

javascript get locale month with full name

JavaScript Date object

The Date object in JS helps you work with dates as it sounds logical. However there are limited number of functions attached to it which you can use. You can check for full Date specification in w3schools : here.

How to get the month name?

Normally when you try to use a date object you can format it with the following code:

var d = new Date();
alert(d.getFullYear());
alert(d.getMonth());
alert(d.getDate());

The problem is that you cannot reference the date object and to get the full name of the month. Something like d.getFullMonth()!

That’s why the easiest way to get the full month name is to make a custom locale array such this one:

var monthLocale = new Array(12);
monthLocale[0] = "January";
monthLocale[1] = "February";
monthLocale[2] = "March";
monthLocale[3] = "April";
monthLocale[4] = "May";
monthLocale[5] = "June";
monthLocale[6] = "July";
monthLocale[7] = "August";
monthLocale[8] = "September";
monthLocale[9] = "October";
monthLocale[10] = "November";
monthLocale[11] = "December";

And than you can simply reference the locale array!