Marcos Placona Blog

Programming, technology and the taming of the web.

Category: JQuery (page 1 of 4)

My studies and jQuery tutorials, as well as browser usability

Want a free copy of my new book?

Reading time: 2 – 3 minutes

No catches here, all you need to do to get a free e-copy of my new book

jQuery Drag-and-Drop Grids How-to (UK , US)

Is read the book, and then write a review of it on your blog.

And I’m not asking you to write a good (or biased) review either. I’m just asking for some feedback. I’ve had a couple of people already writing reviews on their blogs, Google+ and Amazon, but more the merrier. And I want to know what YOU think!

For those who haven’t seen my previous blog post about having published this book, this is my first ever book, and while I have written hundreds of technical articles, none of them have actually made this far on the “publishing chain”.

And this is why I want to have all the feedback I can get. Again, YOU are the sort of person who buys the sort of books I write, and it’s YOU I wanna hear from.

There are 5 free e-copies up for grabs,  and I’ll give it away to the first 5 people who show interest in reading and reviewing my book.

Well, that’s gotta save you some money eh?

So get in touch by leaving a comment to this post, or via contact form with your name and blog URL. I’ll then get you your free copy, and all I ask in return is to be notified when you’ve published your review.

I am a published author

Reading time: 3 – 4 minutes

A few months ago I was contacted by Packt Publishing about a new project they had in mind and thought I’d be a good fit for.

The challenge was to write a book about one single topic that would enable readers to learn and become proficient on that topic in about an afternoon.

Anything with 200 pages would be too much, so I had to keep the book very lean while keeping the reader interested, and giving her enough ground to be able to build a final project just with the information contained on the book.

I had a pre-set topic as well, and had to stick to about 35 pages of content. If you remember, a while ago I wrote an article about how to do drag and drop with jQuery.

While the article was only about dragging and dropping DOM elements, I was asked to write a book on how to drag entire layouts on the screen, and allow users to fully customize their experience.

I set off by providing the publishers with a proposal of what I thought the structure of the book should be, and decided to use Gridster as the plugin for drag and drop.

If you haven’t heard about Gridster, it’s a very nifty jQuery plugin that allows you to do layout drag and dropping, and has lots of cool API functionalities embedded in it that allow you total control over your layout’s mobility, and immediate feedback on all your elements.

On the book, I take the reader through the whole process of downloading and adding the library to their website, until the point they can actually create a fully functional metro styled layout that allows users to drag and change positions on every item on the screen.

So without further ado, here’s my book…

Instant – jQuery Drag-and-Drop Grids How-to

jQuery Drag-and-Drop Grids How-to

jQuery Drag-and-Drop Grids How-to

It’s available on all major stores such as:

Amazon UK

Amazon.com

Safari Books Online and I hear B&N are also stocking it, but sadly it’s not yet available on their website.

Hope you enjoy it! ;-)

Finite image slider

Reading time: 4 – 6 minutes

I’ve been asked to come up with an image slider prototype for something new we were doing at work.

I would generally just find a suitable sliding jQuery plug-in and get the developers to use it. However, this time, there were two very peculiar requirements necessary for this slider (hence why I was asked to prototype it)

  1. The image slider should only display the next and previous arrow when there was something to display. It should hide the arrows if it didn’t have a next or a previous item to display.
  2. Ordinarily image sliders are infinite, which means you can keep clicking next forever, and it will always loop through the images. This alone negates the requirement above, since you always have a previous and a next image. So I had to somehow limit the image slider to only go up to the last image when clicking forward, and stop at the first image when rewinding.

I looked at a few options for this, but most of the image sliders out there seem to not allow you to have full control over the display of the arrows, or to make it stop when it reaches the last images. Also, most of them seem to only open up in a light boxes, which makes it less ideal when you don’t want to distract your users off the main objective of the website (don’t get me wrong though, light boxes are cool, but they have their time and place).

I then came across to Nivo Slider(http://nivo.dev7studios.com/), which claims to be the “The world’s most awesome jQuery & WordPress Image Slider”. I can’t really comment on that statement, but I can tell you they’re pretty good, and have lots of different options for you to use in your image sliders, as well as allowing various transitions between each slide.

Implementing it was very straightforward, as all you need to do is import their css, chose a css theme and the plug-in itself (I’ll assume you already have jQuery installed)

My code then ended up looking like this:

The code above will pretty much get you going, and your images should now load nicely on the screen, and you should be able to slide through them.

However, when you do it, you will notice none of the requirements mentioned above have been fulfilled, since you ended up with an image slider.

To get the arrows to show and hide dynamically, I did a bit of research and found  someone doing that exact same thing. So it was just a matter of adding that to my code, and attaching it to the “afterChange” event, which will get called every time you click through any of the arrows.

I then added it to my configuration as such:

afterChange: function(){arrowsCtrl();}

You will have noticed this only disables the arrows after you change the slides, so you’re still faced with the problem that you load the page displaying the two arrows. In order to remove the arrow from the left (I don’t need a left arrow if I just loaded the page, since I have nothing to go back to), I simply used the event “afterLoad”, which runs as soon as the object is added to the DOM.

afterLoad: function(){jQuery('#slider .nivo-prevNav').hide();},

That way, I can simply hide the arrow using JavaScript.

So the first requirement is fulfilled:

  • The image slider should only display the next and previous arrow when there was something to display. Check
  • It should hide the arrows if it didn’t have a next or a previous item to display. Check

The good news here, is that by doing this, I have automatically satisfied my second requirement, which was to make sure the user couldn’t loop through to the first image when she reached the last image. By removing the arrows when necessary, I am able to stop the user from ever going back to the first image without going through the previous images.

because I was using “slide in” transitions, one thing I found to be a bit annoying, is that you can only define one kind of transition per slide, so when sliding in an image, you normally slide it in from left to right. If you want to slide it out (i.e. by clicking on the right arrow), you’d normally expect the image to disappear from right to left, as if it was leaving the screen.

This is how I’ve implemented it:

$('#slider .nivo-nextNav').live('mouseover', function() {
$('img').attr("data-transition", "slideInLeft");
  });
$('#slider .nivo-prevNav').live('mouseover', function() {
$('img').attr("data-transition", "slideInRight");
  });

My final code ended up looking like this:

View Demo

Nifty jQuery tricks – Ajax Events

Reading time: 2 – 4 minutes

As previously mentioned on my other post, I’m working on improving usability and user experience within one of our applications.

This application has a somehow complex authentication system managed by Fusebox.

In one of our screens, we’ve replaced most of the page refreshes with ajax calls, so on the table of contents, the user can modify anything, and it gets saved automatically upon confirmation without the need for clicking buttons or reloading the page.

Our QA soon noticed that if during the process of browsing the page and updating something you got yourself logged out, the authentication system would kick off (as it runs per request), and the whole page would get a bit screwed.

An example of this would be:

Ajax Driven Page

When clicking on each of the tabs, they would trigger an Ajax call to a dynamic page that would return some HTML. this HTML would fill up the internal div and display the contents without refreshing.

Now, if while clients were viewing this page they decided to go for a cup of tea and clicked on a tab when they came back after 15 minutes, the following would occur:

Ajax Driven page screwing things up

That isn’t the right behaviour, as the browser is supposed to redirect the user to a login page, and not display this page inside the tabs. in this situation, no matter what the client do, clicking the tabs will always take them to the same screen, which could confuse them.

To get around this, I need to have some kind of hook that will check if the user is logged in before any Ajax requests are made.

Come jQuery to the rescue!

When using any of the Ajax capabilities available through jQuery, you can register a handler to be called when the  request begins.

This can be accomplished with ajaxStart(). it will kick off whenever you make an Ajax request, and in this case, it works a treat, as it will go through my authentication system, and redirect the page to the login screen should the user have been logged in.

A simple code sample for this would be:

$(this).ajaxStart(function() {
     $.ajax({
           url: 'index.cfm?fuseaction=c_common.islogged',
           success: function(data, event) {
             //check the results of the authentication page here
           }
     });
});

You can also use ajaxStop() for the exact same effect, but only getting triggered when the Ajax call is finished.

Nifty jQuery tricks – Avoid Cache

Reading time: 2 – 2 minutes

I found this two function yesterday while working on one of our applications.

We are turning a very boring page into a full blown jQuery powered page. It’s got loads of functionalities being turned into Ajax calls to give the user a better experience. We are really taking usability into account, and minimizing things like page refresh or reloads is essential.

One thing that got to us, was the fact that IE8 insists in caching some Ajax contents when loading things too quickly (i.e. if a link is clicked, and a tab is clicked subsequently).

This is easily fixable by adding a timestamp to the request, so whenever a link is clicked, the request would be doing something like:

var tS=new Date().getTime();
$.ajax({
    url: searcher,
    data: {timestamp:tS},
    type: 'GET',
    dataType: 'json',
    success: function(data) {
        //something here
}});

The code above will do the job, but you have to remember to always use the timestamp, and pass it on.
Another better way of doing it, is by using the ajaxSetup function.

According to the documentation, ajaxSetup sets default values for future Ajax requests. So in other words, every Ajax request made after ajaxSetup will obey the directives defined by it.

One of this directives happens to be called “cache”, and if set to “false”, will make sure every Ajax request has the current timestamp appended to the URL. You can simply add it to your code as such:

$(document).ready(function() {
   $.ajaxSetup({ cache: false });
   // put all your jQuery goodness in here.
 });

Now all of your requests will look like this:

http://my_server.com?123456789

There are also other settings that can be added inside ajaxSetup. They can be found on the jQuery.ajax() documentation, and all the settings listed in there will also work with ajaxSetup.
Hope this tip is useful to someone.

Older posts