Posts Tagged ‘JavaScript’

Integrating JSLint for vim

Saturday, October 3rd, 2009

A couple of months ago I moved away from TextMate and started using vim for most of my text editing needs. I love TextMate, but I needed a powerful text editor that is also available on other operating systems.

Overall I love vim: it has a steep learning curve, but it’s very powerful.If you are touch-typing and loath the mouse as much as I do, it’s fantastic. But I found myself missing out on some of TextMate’s great bundles, and JSLint integration is one of them. After some googling around and looking at different solutions, this is the one that was easiest to install and I like the functionality.

First download & install JavaScript Lint. On OSX & Linux, you can do so by copying the resulting folder into your PATH. I put it in /usr/local/bin/.

Then get the JavaScriptLint vim plugin by Joe Stelmach and restart vim.

Off you go. When you save a JavaScript file in vim, you will get a window with JSLint warnings. If you want to configure JSLint to show/hide certain errors, you can edit the config (jsl.default.conf) in the folder. You might want to backup the file before editing it.

Happy linting!

jQuery list paginator plugin – Elpaginator

Monday, January 26th, 2009

I know it’s been a while. I started about 5 posts, but haven’t finished any of them… 

Hopefully this makes up for it: as a treat for the new year, I thought a little jQuery plugin that I wrote would be handy for some people: please welcome Elpaginator!

What it does:

It looks for a ul or ol within a wrapper & adds pagination depending on the maximum number of items specified.

Features:

  • paginates if there are more items than the user specified
  • displays number of items left or pages, i.e. 1-10, 11-20, etc or 1, 2, etc
  • html of pagination is fully configurable (wrapper need to contain class of page-list!)
  • the number of the list item if ordered list is used is correct (uses start attribute that is deprecated, but there is no real alternative yet as IE6 & 7 don’t support CSS for this yet)
  • support for ul & ol
  • very lightweight (less than 200 lines WITH lots of comments)

 

You can find the elpaginator script & a demo page in my repository.

Options

This is how you initialise the pagination:

  $('#wrapper').elpaginator();

Possible Options are:

  $('#wrapper').elpaginator({
    MAX: 10,  // maximum number of items per pagination
    list: 'ol',   // whether it's an ul or ol. Default: ul
    style: 'items', // other value is pages -> show 1-5 6-10, etc, or 1, 2,
    paginationHTML: '<div class="pagination"><ul class="page-list"></ul></div>', // HTML for the pagination. Is appended to the wrapper
    paginationListItemHTML: '<li><a href="#"></a></li>' // HTML for the pagination items
  });

A live example of it working can also be seen on the new Top Gear site (look at the sidebar for the lap times table).

Splitting a long JavaScript String across multiple lines

Wednesday, November 12th, 2008

Whilst reading a comment by Mike DeBoer on Ajaxian about delimiting JavaScript strings with backslashes, I thought it’s worth mentioning it here as well, if only to remind myself.

String concatenation always creates a new string.

Thus, writing:

 
var str = "This is a long" +
  " string. That's why we want to " +
  " split it over several lines";
 

is slower & more memory intensive than writing:

 
var str = "This is a long\
 string. That's why we want to\
 split it over several lines";
 

Just be careful you don’t have trailing spaces as that will mess it up.

JavaScript cookies made easy

Tuesday, November 4th, 2008

Here is another little script that makes my life a bit easier. It consists of 3 methods that makes working with cookies very straight forward. This script is built on the shoulder of giants: PPK from Quircksmode was the man who did the hard work.

However, rather than creating 3 methods that sit in the global namespace, I wanted to use a singleton object that would only add 1 global variable. Since there is no need for private methods, the object literal pattern is appropriate.
In my opinion the names of the methods make it very straight forward to use. Good names mean less looking up, mean less mistakes, mean faster development. Your Technical Project Manager will love you for it :-)

To create/set a cookie:
cookie.set('name', 'value, 234); // last argument is number of days until expiry. If none is provided, it will get deleted when the browser is closed

To get the cookie value:
cookie.get('name');

To delete the cookie:
cookie.erase('name');

As always, you can download the latest version from my svn repository

jQuery plugin autoInputValue – default input values made easy

Saturday, October 25th, 2008

We all know the problem: the designers are struggling to get the form elements in the space they have or they decide that having labels next to input fields is not really necessary…
Instead the label is displayed inside the input field & supposed to disappear when the user clicks or focuses on the field.

I’ve come across this quite often lately, so I quickly put a little jQuery plugin together to make my life a bit easier.

It works on input fields and it works just like any other jQuery plugin.

All you need to do is call the plugin with the selectors you are interested in, i.e.
$('input:text.email').autoInputLabel();

The options are specified in an object literal & can be:

  • blurColor – colour of the text when the label is displayed
  • focusColor – colour of the active text, i.e. user input
  • value – the label that should be displayed

By default, the plugin displays the value of the value attribute. If there is none, it checks whether there has been a value provided by the user. Otherwise it looks for the label associated with the input field (that is probably hidden, otherwise why would you use the plugin?). The label needs to be associated to the input field with the for attribute.

The plugin only works on <input type="text"> fields.

You can download the latest version from my public svn repository