jQuery AJAX trick (very useful!)

3 Jul

Did you ever ask yourself how can you display a “loading” indicator when you make ajax calls using various libraries?

The simple but efficient answer is this:

1
2
3
4
5
6
$(document).ready(function(){
$('<div id="busy">Loading...</div>')
        .ajaxStart(function() {$(this).show();})
        .ajaxStop(function() {$(this).hide();})
        .appendTo('body');
});

And that’s all. You need to do this only once.

Ofcourse, you can apply some CSS styles to #busy div, using an image generated with ajaxload or something similar.

I didn’t use any other libraries (actually i used prototype with scriptaculous sometime like AGES ago), but i think all cool libraries (like jQuery) have similar methods.

Another great news: with MORE than 50 fixes (but no new features tough) jQuery UI 1.5.1 is out.

Tags: , , Comments (1)

Bookmark this article!

Del.icio.usDiggStumbleUponFurlRedditTechnorati

SlashDotWindows LiveYahooGoogleFacebookBlogLines

Best way to CSS hacks

17 May

Every time you code a layout, you must dealing with inconsistencies of various browsers. Due to his lack of standard support, IE (and especially IE6) has a LOT of css hacks available “on the market”. And that because the IE developer team has some kind of humor for two reason:

  1. They know that their browser suck so they put conditional comments. Is not a really hack, because you have a documentation, but is the best (and also the safest) way to fix CSS bugs (or even add some extra content for certain version of IE).
  2. The second reason is funny and creepy: the fresh SP3 for windows is delivered with… IE6. Yes, if you don’t have IE7 installed, you will have IE6. That sucks, isn’t it?

Ok, enough with this, let’s show some real hacks, right?

Internet Explorer hacks

First of all: the best way is to use conditional hacks for IE. That’s because new version of other browsers can misunderstanding some hacks. As far as i know, this wasn’t happen yet, but you never know what gift we can get, right? Use hacks when you have only a couple to fixes!

The most used hack for IE6 is the star selector :

#wrap {border:2px solid blue}
* html #wrap {border:5px solid red}

For IE6, wrap container will have a 5 px border. For any other browser, you will get a 2px blue border. Is important to get IE6 hack AFTER the normal rules, otherwise you won’t get any result.

html>;body #wrap {background:white}
#wrap {background:red}

This one is some kind of first rule, but reverted. That’s because IE6 simply don’t know how to deal with this kind of selectors (which is valid tough!). Lucky us, right?

Another hack is the underlined and !important. However, those hacks are NOT recommended for mass production (is ok for testing, but… that’s all):

#wrap {
color:red !important;
color:blue
}

and

#wrap {
color:red;
_color:blue
}

With both hacks you will achieve same result. But i repeat, DO NOT USE it unless you know what are doing. You will avoid a LOT of headaches ;)

Until now, i talk about IE6 hacks. Here you have a IE7 hack:

#header { font-size:1em }
*:first-child+html #header { font-size:2em }

Like i said before, is mandatory that hack to be AFTER proper rule!

IE6 min/max-width hacks

#wrap {
	width: expression(document.body.clientWidth <; 742? "740px" : document.body.clientWidth >; 1202? "1200px" : "auto");
	}

For this one, we need to use some CSS expressions. Also, is not recommended (can affect user experience, because browser execute that expression). This hack was founded here.

Image flicker problem

One of the most annoying thing on IE is this: when you have a css image on a link, when you hover that link, the image is reloaded, resulting a short time flicker. Mister-pixel.com comes with a handy solution for this (that works only in IE6 SP1 or greater):

<script type="text/javascript" charset="utf-8">
/* <![CDATA[ */
	try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}
/* ]]> */
</script>

You only need to put these lines in your header (inside of conditional tags) and your problem is done.

There is more?

Well… I’m sure i didn’t cover all CSS hacks. However, these are some of the most important hacks, ready to use anytime you need it (and for your own good, you should bookmark this page because you will need it A LOT!). Do you know any other hacks?

Tags: Comments (1)

Bookmark this article!

Del.icio.usDiggStumbleUponFurlRedditTechnorati

SlashDotWindows LiveYahooGoogleFacebookBlogLines

Best practices and advices

15 May

As i said in previous post, i have some tips and advices for

Use CSS reset

I personally don’t use any of existing CSS reset file and i use only few line for this, including the blamed * {margin:0; padding:0} technique:

* {margin:0;padding:0}
img, fieldset {border:none}
table {border-collapse:collapse}
td, th {text-align:left;vertical-align:top}
button {cursor:pointer}
input, button, select {vertical-align:middle}
textarea {overflow:auto}
 
html { font-size:100.01%; }
body { font-size:1em; }
 
a {text-decoration:none}
a:hover {text-decoration:underline}
 
input.text, input.password, textarea {border: 1px solid #7f9db9}
 
.clearfix:after {content:".";display:block;height:0;clear:both;visibility:hidden}
.clearfix {display:inline-block}
/* Hide from IE Mac \*/
.clearfix {display:block}
/* End hide from IE Mac */
* html .clearfix {height:1px}

Not only i reset all styles, but i also use a smart way for clearing floats. Just add class=”clearfix” to some floatet box container and here you go. No more clear both extra tag, no more other tricks.

Don’t get me wrong. Every time i’m in a middle of a project i remember to NOT use * {} CSS reset for next project. And every time i forgot :(

(Don’t) Use CSS frameworks

I don’t like to use code that is not mine. And CSS frameworks is this kind of code. Beside that, when you start to use a framework (no matter what framework is), you MUST learn it. Too much waste of time to learn another people code. Again, you have plenty of CSS frameworks to pick and try.

Validate your code and search for browser bug

Some times a page is not displayed well. Either you have a bug in your code or bug in your favorite browser, you need to figure in a way or other what is guilty for headaches. The best way to eliminate code bugs is to validate your code. If all lines is ok, you should search for bugs in browsers.

Comment your code

You probably say: why should i bother to comment my own code? I wrote it, i will remember! Yeah, sure! Over the past years i convinced myself how much i can be WRONG! I try to read some past code (yes, written by myself) and i ask: what the heck i wanted to do here? So now i comment my code: CSS i split in blocks and on html i comment the closed div/class for big containers:

<div id="wrap">
<div class="header">
  content goes here
</div><!--/.header-->
</div><!--/wrap-->

Read news, blogs and forums

I have a many RSS feeds in Google Reader and half of them is about development. CSS, Javascript or only news, is all good. Of course, i don’t spend too much time reading all articles. I read only headers and if is worthing, i read it all. Is the BEST way to stay informed about web technologies.

Test in many browsers

Don’t matter if you made a website for yourself or for a client, because that site MUST be rendered in the same way in ALL browsers. Or at least to degrade graceful and remain usable.

Use your own javascript vs. libraries

Well, if you read constantly this blog, you know that i’m in love with jQuery :D And for most projects i need speed in development and just to be sure i achieve same results in all browsers. I can use my own JS or i can use my own JS powered by jQuery. The second way is always preferred :D

More to come?

Sure! If i remember any other tips, i will surely tell you! Just add this blog to your reader and stay tuned!

Tags: Comments (7)

Bookmark this article!

Del.icio.usDiggStumbleUponFurlRedditTechnorati

SlashDotWindows LiveYahooGoogleFacebookBlogLines

Best tools for development

15 May

The most difficult part for a beginner developer is to find right tools for him. In fact, all developers aims straight to speed, right? For a front end developer, all basic tools you need is a text editor and many (as many as possible) browsers. At least in theory.

In real life, developer needs more than that. Here is a more than basic list with what programs i use:

  • Adobe Photoshop. There is versions for both Mac and Win (no Linux; i will tell you later why) and it’s do his job very well. If is too big for what you need (and almost every time is too big), you can try his little brother Fireworks. You need only a soft for making slice and play with layers, so if you can find another software for doing this, let me know.
  • Text editor. There is a PLENTY of editors, one better than other and you only need to pick one. Ofcourse, you don’t have to be stuck with that editor because anytime you can find a better one. I used Dreamweaver for about one year (or even more). I found UEstudio and i used for another year or so and last year i found a great TextMate port for Windows: e-Texteditor. And for now i think is the best (still having bad parts, but if i put side by side all editors, e is the ONE) and for now i don’t think to change it. The best thing is if you use e for a while and you want to switch on Mac with all your e custom bundles and themes, just DO IT! Working like a charm. Ofcourse, you can find a HUGE number of editors: Aptana, Zend (yes, some guys are using this not only for PHP), Eclipse, Intype (is some kind of e or textmate), Komodo, Maguma, GoLive!, Notepad++ and so on.
  • VMWare, Parallels, VirtualPC or any other software that will allow you to run another OS in your current OS. Again, there is versions for both Mac and PC. This is to be sure that your site will be displayed like you want in ALL browser (or as many as possible). I’m sure you don’t want a site that is looking good on IE7 and is screwed up in IE6, right? I use Vista as main OS, OsX as secondary OS (yes, Hackintosh) and Windows XP with IE6 and Ubuntu 8.08 on my virtual machine.
  • Apache, php and mysql. Either you installing this manually or install a all-in-one package (Xammp, EasyPHP), you probably need a server like that if you plan to develop themes for CMS/Forums or Blogs. For Movable type, for example, you also need too install Perl.
  • If you are working on big projects with many developers, you should consider to use SVN and a SVN client.
  • Patience. A HUGE dose of patience, because will be a LOT of stuff that will simply won’t work on some browser (usually IE6) and if you don’t know what to do, you will get some white hair before the time :D
  • A music player :D I don’t know how do you code, but i can’t code in absolutely silence. I search for a radio station and just play at a low volume.

As i said before, i will tell you why you don’t have some of these tools on you Linux box (but maybe you have many other great tools): there is a tremendous number of Linux distribution with a huge number of configuration and Adobe (and many other companies) don’t want to spend time with technical support (just imagine a call center operator who MUST resolve problems of 20 distro/day!). For front end development i personally think is a MUST to have Windows or Osx (or even both).

Soon i will post a guide with best practices and some advices for front end developers.

Tags: Comments (3)

Bookmark this article!

Del.icio.usDiggStumbleUponFurlRedditTechnorati

SlashDotWindows LiveYahooGoogleFacebookBlogLines