Zepto.js and the :selected and :checked selectors

If you switched from jQuery to its slightly anemic cousin, Zepto.js, you might have noticed that it doesn’t support two rather useful selectors, :selected and :checked. Fortunately, it is fairly easy to compensate for it. First, define the following two functions: var checked = function(){ return this.checked; } var selected = function(){ return this.selected; } […]

How to implement parallax scrolling with jQuery

A parallax effect is adds a nice touch of style to an existing website, and looks fantastic with large images. Here is how to implement a parallax on a background image with jQuery. First, set the element’s background to fixed: .myElement{background:url(‘images/myBackground.jpg’) no-repeat fixed center top} Next, put this in your $(document).ready() function. Do not forget […]

How to fade images on load with jQuery

In this article, I will show you how to properly fade in images when the page loads using jQuery. Although this has been covered by other blogs, most proposed solutions do not take caching into account. When returning on a page, images that were cached by the browser will not trigger jQuery’s load event. First, […]

How to convert Javascript dates to UTC

When you create a JavaScript Date object, it will set the date in your time zone. If you want to keep the same date, but convert it to UTC/GMT for storage and comparison purposes, use the function below: //Converts a Date object to its equivalent in UTC time. function dateToUTC(date) { return new Date(date.getUTCFullYear(), date.getUTCMonth(), […]

How to parse a JSON date with Javascript

Parsing a JSON date is rather easy, and doesn’t even require jQuery. Since the JSON date format is always the same, you can use substr to retrieve the timestamp and parse it to a date. var date = new Date(parseInt(jsonDate.substr(6))); There’s nothing more to it; it’s that easy!

Adding days to a Javascript Date object

Adding an arbitrary amount of days is easy with Javascript. Using the setDays() method, it’s pretty self-explanatory: //Adds a day to today var today = new Date(); var tomorrow = new Date(); tomorrow.setDate(today.getDate()+1); //Tomorrow = today + 1 day

How to isolate the date from a Date object in Javascript

Sometimes, you will need only the date part from a Date object. In order to truncate the time from the date, here is what to do: //Create the date object myDate = new Date(); //Current date and time myDate.setHours(0,0,0,0); //Current date at midnight If you have any questions about JavaScript dates, feel free to ask […]