balupton/history.js — Issue #134: Console.log(state) spam
Author: intellix
Created: 2011-11-30 10:14:09+00:00
State: open

Got a weird sort of spam of the console on the statechange event. (I'm using jQuery 1.71 and Chrome 15).

I'm not quite sure if its a bug but it's weird so if its normal then I guess some documentation regarding it as I thought I was doing this wrong. May even be a bug! who knows... so anyway

$('nav a').click(function (e) {

var url = $(this).attr('href'),
title = $(this).attr('title') || null,
category = self.getCategoryFromUrl(url);

// Push new state into History.js plugin
History.pushState({tab:category}, title, url);

e.preventDefault();
return false;
});

$(window).on('statechange', function() {
console.log(History.getState());
});

So if you click an anchor, you'll see 1 state is logged after the other as its getting pushed in... great...
Press the back button and it logs one, press it again and now there are 2 logged? press it again and now it adds 3?

At first I thought that the statechange event was firing multiple times and was building up so I thought that I needed to replaceState instead of push but if you clear your console between pressing the back button then you only receive the one logged state. Is this a browser issue where its sort of remembering the first log or is the statechange event firing multiple times? I'm not entirely sure but could do with documenting it.

I thought I was doing it wrong and figured maybe it would be nice to have a more real-world example of this in use. A menu with 5 links and 5 pieces of content.