views:

193

answers:

4

I'm getting more into jQuery and so have set up a HTML/Javascript/CSS base site which I use for all my tests.

Since these tests will eventually turn into PHP and ASP.NET MVC websites, I want to use this opportunity to get the basics down right again for modern browsers and web standards before building the scripting languages on top of it.

I've selected to use:

  • XHTML 1.0 Strict
  • UTF-8 encoding
  • as few CSS references as possible (put everything in 1 CSS file for loading speed)
  • as few Javascript references as possible (1 javascript file plus the jquery code base reference - I assume using the Google jQuery code base is best practice for speed)
  • I check my code as I build it with the http://validator.w3.org

Is there anything else I need to consider?

Here is an example of one of my test websites:

index.htm:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;

    <head>
     <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
     <title>Text XHTML Page</title>
     <link href="css/main.css" rel="stylesheet" type="text/css" media="all"/>
     <script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
     <script type="text/javascript" src="javascript/main.js"></script>  

    </head>

<body>
    <h1 class="highlightTitle">Text</h1>
    <p class="main">First</p>
    <p>Second</p>
    <p id="selected" class="regular">Third</p>
    <p>Fourth</p>

<form action="">
    <div>
     <input type="button" value="highlight it" onclick="highlightIt();countThem()" /> 
     <input type="button" value="highlight title" onclick="highlightTitle()" />
     <p>here is another paragraph</p>
    </div>
</form>

</body>
</html>

main.cs:

p.highlighted {
    background-color:orange;
}
h1.highlightTitle {
    background-color:yellow;
}
h1.deselected {
    background-color:#eee;
}
p.regular {
    font-weight: bold;
}

main.js:

google.load("jquery", "1.3.2");

function highlightIt() {
    $('#selected')
     .toggleClass('highlighted');
}

function countThem() {
    alert("there are " + $("p.main").size() + " paragraphs");
}

function highlightTitle() {
    $("h1").toggleClass("deselected");
}
+4  A: 

Move the <script> blocks to the bottom of the page.

Rob
hmm, I explicitly moved them to the top of the page again after having had experience of jquery effects not working until the page was fully loaded, e.g. I had a flashcard site where the user had to wait for the full page to load (5 seconds) before behind able to flip flashcards over, but I keep hearing this is best practice, but I it can't be that bad if sites like http://www.apple.com have 12 (twelve) script references at the top of the page.
Edward Tanguay
Browsers generally handle loading javascript one file at a time. Your page will usually load faster if you move them to the end since all of the other requests will be handled in parallel before the javascript loading starts. If your page takes a really long time to load and the interface doesn't function properly until it does so, you may want to have a loading message that is visible with the real content hidden until the interface is available. The last bit of javascript loaded would contain code to remove the loading image and reveal the interface.
tvanfosson
+5  A: 

Personally I would bind to the click event via jQuery to ensure nice separation, like this:

$("#yourId").bind("click", highlightIt);

This way you can bind to multiple event handlers. If you just use onclick AFAIK you can only ever use one handler.

BTW you can also use custom event and event namespaces:

$("#yourId").bind("beforeHighlighting", doSomething);

is triggered by

$("#yourId").trigger("beforeHighlighting");

and

$(".hasAHelptext").bind("helptext.click", showHelptextFct);
$(".hasAHelptext").bind("click", otherFct);

// will only remove the showHelptextFct event handler
$(".hasAHelptext").unbind("helptext.click");

HTH Alex

AlexDuggleby
thanks, yes, unobtrusive javascript is another design goal that I'll stick to as I build these examples, just hadn't made it into this example yet :-(
Edward Tanguay
+3  A: 

With regard to CSS and JS files in general, I wouldn't combine all JS files to a single file during development. It gets very hard to develop in one big JS file. Rather use a module that combines them on-the-fly or during deployment.

I usually go with (both CSS and JS):

one general file:

  • project.css

and one per page:

  • project_welcome.css

and any special components (login controls, ad area views etc) have a seperate one as well.

That way you can apply some organizing techniques and won't go crazy managing that single large file.

HTH Alex

AlexDuggleby
Seconded, having fewer files won't bring any speed advantage, I would bet on the opposite. It's all cached anyway after the first request.
simon
Hmmm, I used to have dozens of javascript files in my PHP websites but decided to switch to one now since I keep reading that the "two simultaneous downloads per hostname" issue is one of the biggest speed bottlenecks for websites, not true?
Edward Tanguay
yes I can confirm that if you use a similar approach as I mentioned yo u can easily end up with the browser trying to load 50+ JS + CSS files which on the first hit will cause it to take a long time. And after each update the same happens etc... Of course the cache will kick-in after that, but that doesn't matter if your visitor doesn't come back after the first loading time took too long. And things like minifying the JS is easier once you've processed it into one single JS file.
AlexDuggleby
Use whatever number of individual files is appropriate during development. In deployment, you might consider using a tool that will concatenate your scripts together and minify them (i.e. remove useless whitespace and other trickery to reduce the file size). You might also think about using mod_gzip (or the appropriate equivalent) to serve the file compressed, where the browser supports it.
Rob
+1 for gzip and minification: http://developer.yahoo.com/performance/rules.html#gzip, http://developer.yahoo.com/performance/rules.html#minify
Rich Seller
A: 

I would recommend putting the JS calls below the body tag. If your scripts are hanging, then the page can load and let the behavior (JS) load after the fact. I've noticed that speed greatly improves with this method.

Check this out: http://stevesouders.com/hpws/rule-js-bottom.php

mager