Get width of scrollbar

A snippet of code to get the width of the scrollbar

HTML

<div id="test">
  Width of scrollbar: <span style="font-weight: bold;" />
</div>

JavaScript

// Get scrollbar width
var inner = $('<p />', {
  style: 'width:100%;height:200px;'
}),
  outer = $('<div />', {
    style: 'position:absolute;top:0;left:0;visibility:hidden;width:200px;height:150px;overflow:hidden;'
  }),
  body = $("body");

outer.append(inner).appendTo(body);

var w1 = inner.get(0).offsetWidth;
outer.css('overflow', 'scroll');
var w2 = inner.get(0).offsetWidth;
if (w1 == w2) w2 = outer.get(0).clientWidth;
outer.remove();

var w3 = w1 - w2,
  test = $("#test > span");

test.text(w3);