Character Encoding

In this compact post I am addressing two issues:

  1. Using UTF-8 as a character encoding
  2. Position of charset meta tag in html document

Too many websites are still using a character encoding other than utf-8.
Too many websites don’t have the charset-meta tag as first element within the <head>-section.

According to w3 the meta tag for the charset needs to be placed as early as possible in the html document and should be the first element within the <head>-section. You can find examples of how to do this for HTML5, XTML, HTML4 on W3′s Q&A about doctype declaration. It makes sense to add this tag even before the <title>-tag since the title may contain Umlaut or other character which are not part of ASCII. UTF-8 is good for multilingual support.

The META declaration must only be used when the character encoding is organized such that ASCII-valued bytes stand for ASCII characters (at least until the META element is parsed). META declarations should appear as early as possible in the HEAD element.

Source: http://www.w3.org/TR/html4/charset.html

Examples

Twitter Bootstrap

Notice the first element after <head>:

<!DOCTYPE html>
<html lang=”en”>
  <head>
    <meta charset=”utf-8″>
    <title>Bootstrap, from Twitter</title>

Source: https://github.com/twitter/bootstrap/blob/master/examples/container-app.html

HTML 5 Boilerplate

Notice the first element after <head>:

<!doctype html>
<!– paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ –>
<!–[if lt IE 7]> <html lang=”en”> <![endif]–>
<!–[if IE 7]>    <html lang=”en”> <![endif]–>
<!–[if IE 8]>    <html lang=”en”> <![endif]–>
<!–[if gt IE 8]><!–> <html lang=”en”> <!–<![endif]–>
<head>
<meta charset=”utf-8″>

Source: http://html5boilerplate.com/

Conclusion

  • Always specify a character set.
  • Use UTF-8.
  • Place the meta tag immediately after the head-tag

Further reading / Sources used

Posted in frontend, quicktip, Uncategorized | Tagged , , | Leave a comment

Using TerrificJS in your website

I have set up a few projects with TerrificJS and I have to say it’s great! With TerrificJS I don’t have to wonder about how to organize my javascript source code. The structure is taken care of. The timing is taken care of. There’s no danger of one module’s code getting in the way of another. So what’s a module? TerrificJS handles modules much like OOCSS. OOCSS is a CSS framework which gives common components or modules (I’ll stick with module) a blueprint structure.

Here’s a minimum module structure (see OOCSS Modules for more details).

1
2
3
4
5
6
7
8
<div class="mod MyModulenName"> 
    <div class="inner">
        <div class="bd">
            My Module contents goes here.
        </div>
    </div> 
</div>
</code>

What if you would like to use TerrificJS in your existing website which does not use the OOCSS structure for it’s modules? Doesn’t matter – you can, with RegisterModule. More of that later in this post.

Adding TerrificJS to your website

You will need to download jQuery and TerrificJS:

Include both files, first jQuery and the TerrificJS. You’ll

1
2
3
4
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js" ></script>
        <script type="text/javascript" src="/yourpath/terrific-1.0.0.min.js" ></script>
    </body>
</html>

The Bootstrap

jQuery and TerrificJS are included. Next the TerrificJS Bootstrap needs to be added.

1
2
3
4
5
6
7
8
9
10
11
12
13
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js" ></script>
<script type="text/javascript" src="/yourpath/terrific-1.0.0.min.js" ></script>
<script type="text/javascript">
	// TerrificJS bootstrap
    (function($) {
        $(document).ready(function() {
           var $page = $('body');
           var application = new Tc.Application($page);
           application.registerModules();
           application.start();
         });
     })(Tc.$);
</script>

The TerrificJS Bootstrap is added after the jQuery and TerrificJS libraries. A lot is happening here.

jQuery noConflict

First of all TerrificJS makes use of jQuery noConflict (also have a look at the TerrificJS uncompressed source code).

Tc.$ = jQuery.noConflict(false); // Part of TerrificJS source

This explains the self-executing function which begins on line 4 and ends on line 11. Tc.$, which is a reference to jQuery, is passed on to $. Inside the self-executing function $ can be used to access jQuery. This saves on code and time.

If you need jQuery to be assigned to the dollar variable $ then add the following line after the TerrificJS bootstrap code:

$ = Tc.$ // This goes after the TerrificJS bootstrap

Application context

Line 6 sets the context. If nothing else, the html body containing the visible part of your website is the context.

var $page = $('body');

You could apply TerrificJS to a much smaller area on your website if you don’t want the whole DOM to be scanned by TerrificJS.

var $page = $('#someOtherContainerActingAsContext');

Getting the party started

Lines 7 and 8 initialize the application/website. The context is passed to Tc.Application and all modules are registered. This is where OOCSS comes in. TerrificJS looks for all html elements with the css class mod. It then looks for a second css class with the prefix modMyModuleName. You can see all the naming conventions used by TerrificJS on http://www.terrifically.org/learn/basics/conventions. Does this mean you need to add a mod and modMyModuleName to all your already existing modules on your website? No. Carry on reading.

Using TerrificJS without OOCSS

You’re not starting from scratch and would like to add TerrificJS to your existing website. No problem. Let’s say you your main navigation html looks like this.

1
2
3
4
5
6
7
<div id="MainNav">
	<ul>
		<li><a href="#home">Home</a></li>
		<li><a href="#about">About</a></li>
		<li><a href="#contact">Contact</a></li>
	</ul>
</div>

To treat the div with the id MainNav as a module the following line needs to be added to the TerrificJS Bootstrap.

application.registerModule($('#MainNav'), 'MainNav');

The first parameter is the jQuery object of the module and also the jQuery context used. The second parameter is the name of javascript class used by TerrificJS. For more information on the parameters see registermodule.

Module Javascript Class

The predefined methods used in the code below are explained on http://www.terrifically.org/learn/module. Putting it all together:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(function($) {
    Tc.Module.MainNav = Tc.Module.extend({
 
        init: function($ctx, sandbox, modId) {
            // call base constructor
            this._super($ctx, sandbox, modId);
        },
 
        dependencies: function() {
            //this.require('File.js', 'util', 'beforeBinding');
        },
 
 
        beforeBinding: function(callback) {
            var $ctx = this.$ctx;
            // Put code that only depends on the existance of this dom readyness of this module
 
            callback();
        },
 
        onBinding: function() {
            var $ctx = this.$ctx;
            // Put all your code in here that also depends on the dom readyness of other modules. For example if this module communications with other modules via connectors.
            // You're on the safe side if you put all your code here.
 
            // Example: Change the background color of this 
            // instance of div#MainNav
            $ctx.css('background-color', 'red');
 
        },
 
 
        afterBinding: function() {
            var $ctx = this.$ctx;
 
        }
 
    });
})(Tc.$);

The class name is set on Line 2 (MainNav).

Tip: You could save the above javascript in a file called Tc.Module.MainNav.js and included after the TerrificJS Bootstrap.

Conclusion

So, this is who you can integrate and use TerrificJS on your website. Feel free to contact me if you have any questions or want to see more specific examples.

Posted in jquery, terrific | Tagged , , , | Leave a comment

The Swiss Opendata Manifest

I’m very fond of the Swiss Opendata Manifest (German). It’s written to the point and sums up everything that open data should be about. I’m going take out and quote the interesting bits.

The manifest is divided into five sections:

  1. Initial position
  2. Uses
  3. Realisation
  4. Need for action
  5. Commitment

Initial position

This is a Swiss point of view. Where are we now? Well, government data is being collected, stored and analysed digitally for quite a while now. In the past years, laws about freedom of information (German) were established and information and communication technologies have greatly improved. Official documents must be provided and made accessibly to anyone interested (unless the law states otherwise).

Uses

The data collected and stored by the government and the municipalities can have far more uses the first expected. Government data that is not explicitly protected by law must be made open. Open Government Data is about actively making data open increasing the transparency, enabling innovation and saving money.

Transparency

The citizen has the right to see and understand. Through the many eyes of the citizens there is more control of the data and less work for the administration. Erroneous or extreme values will be spotted much quicker and the quality of the data will increase. Transparency is an essential condition for a successful relationship between the public and administration.

"Transparency is a minimum standard. Data should only be kept from the public for very good reasons such as the protection of privacy and personal data."

Innovation

The open public authorities data can be used by companies and private persons. It’s foreseeable that software engineers and other creative people will use the data to create applications to analyse and visualise the data in an innovative way.

"The information society cannot let this innovation potential to move by without action."

Saving cost

Cleaning, optimising, combining and storing data is a tedious task. If open, the public can join in on the task helping authorities reduce cost.

"The public authorities cannot afford to do without the public’s help and knowledge."

Source

Posted in open data | Tagged | Leave a comment

Improve your frontend engineering skills

If you want to improve your frontend engineering skills and help others at the same time, register yourself on stackoverflow.com and start answering questions. Pick out all the html, css and javascript topics. You’ll learn a lot from other people’s questions and code samples.

Posted in tip | Tagged , , | Leave a comment

Clean JSON

Just the other day I was asked about the following javascript error and why it was occurring:

invalid label
{"completed_in":0.171,"max_id":1009807...e":10,"since_id":0,"since_id_str":"0"}

If you look a little closer you’ll find the syntax error, however a much faster and easier way is to simply copy and paste the above json string into the textarea on http://jsonlint.com/.

Posted in javascript, tip | Tagged , , | Leave a comment

jQuery’s data method

Not sure how I missed this one. Thanks go to Marc for making me aware.. Using jQuery’s data method one can attach any type of information to a jQuery object. Since jQuery 1.4.3 the data method initially reads its data from the HTML5 data-attribute if set.

As of jQuery 1.4.3 HTML 5 data- attributes will be automatically pulled in to jQuery’s data object. The treatment of attributes with embedded dashes was changed in jQuery 1.6 to conform to the W3C HTML5 specification.

Source: http://api.jquery.com/data

Unlike the

1
.attr()

method which can be used to read and write HTML-attributes,

1
.data()

tries to convert the value to a JavaScript type, such as boolean, number, object, array or null. Unfortunately data-attributes won’t be set with

1
.data( key,value )

.

I have prepare a simple example using jsFiddle:

For detailed information have a look at the desctription of

1
.data( key )

under http://api.jquery.com/data.

Posted in javascript, jquery, tip | Tagged , , | Leave a comment

Running multiple Versions of Firefox on one machine

How-To Instructions

In order to test a website with all current major Firefox versions on Mac OS X follow these instructions from Civicactions:

  1. Download Firefox 4, but instead of dragging the program icon into your Applications folder, drag it onto the desktop.
  2. Rename the program “Firefox4” or something similar, to distinguish it from your currently installed version. Now you can move it to the Applications folder.
  3. Do not start Firefox 4 yet. We need to create a new profile first. Open up Terminal (Applications -> Utilities -> Terminal) and enter the following commands:

    cd /Applications/Firefox4.app/Contents/MacOS/
    ./firefox-bin -ProfileManager

  4. Click “Create Profile” and follow the instructions. You only need to enter a new name, everything else can stay as a default.
    Select your new profile to start Firefox. If you don’t want to run this command every time you start, you can uncheck “Don’t ask at startup” so you can always choose when launching either version.

Source: Running Multiple Versions of Firefox on Mac OS X, Sam Lerner, 29.3.2011.

Getting the Versions

I wasn’t able to find a download page for older version of Firefox on the official website, but you can download using the following URL:

Firefox 3.6

http://www.mozilla.com/en-US/products/download.html?product=firefox-3.6&os=osx&lang=en-US

Firefox 4.0

http://www.mozilla.com/en-US/products/download.html?product=firefox-4.0&os=osx&lang=en-US

Firefox 5.0

http://www.mozilla.com/en-US/products/download.html?product=firefox-5.0&os=osx&lang=en-US

Posted in browser, firefox, quicktip | Tagged , , | Leave a comment

It’s been ages

I haven’t posted for ages due to the fact that I’ve been busy with studies and work.

There are a couple of things I’d like to write about in detail, but until time lets me I’m going to post a short list of interesting topics that I’m currently looking:

CSS Lint

The css version of Douglas Crockford’s JSLint. I use JSLint a lot for testing, debugging but also for keeping my javascript code readable and more maintainable. CSS Lint does similar. It is a creation of Javascript Guru Nicolas Zakas and CSS Guru Nicole Sullivan (OOCSS).

I haven’t used CSS Lint yet but I’m planing on integrating it in all future projects.

JSHint

As I mentioned above I am a huge fan of JSLint. The first few days with JSLint can be tough but with time the JSLint warnings get less and less. JSLint is in some ways limiting and for this reason it was forked and the fork renamed to JSHint.

Terrific currently uses JSLint for javascript validation but we’re planing on replacing lint with hint.

Terrific

Terrific has finally be released as an open source project. At Namics we’ve been using Terrific for projects for a more than a year now. Terrific is a framework for Frontend Engineers and aids in developing interactive modules and keeps javascript code organized. It relies on jQuery and OOCSS conventions.

See you later
- tj

Posted in frontend | Tagged , , , , | Leave a comment

My Links of Week 7

Things have been busy lately and work is plenty, so I’m keeping the blogging to a minimum.

Some of these links are older, but they are all handy sources, which I’ve actually needed this past week.

Deploying without rails:
http://ryanflorence.com/deploying-with-capistrano-without-rails/

Crossbrowser-compatible opacity:
http://davidwalsh.name/css-opacity

jQuery’s $.extend is slow:
http://trevmex.com/post/2531629773/jquerys-extend-is-slow

IE6 image flicker issue solved:
http://ajaxian.com/archives/no-more-ie6-background-flicker

CSS text-overflow property:
https://developer.mozilla.org/En/CSS/Text-overflow

Have a good one!

- tj

Posted in misc | Tagged | Leave a comment

jQuery tip – finding elements with or without an attribute

Sometimes you need to find all uses of an element with or without a certain attribute. In my case, I was looking for all images that didn’t have an alt-attribute.

I used the firebug console and the following line of code to find all img-tags without or with an empty alt-attribute:

console.info(jQuery('img, input[type="image"]').filter(':not([alt])').length);

So that’s how it’s done.

Keep rocking!

Posted in javascript, jquery | Tagged , , | Leave a comment