JavaScript str_pad function

JavaScript equivalent of str_pad function for PHP

function strPad(input, length, string) {
    string = string || '0'; input = input + '';
    return input.length >= length ? input : new Array(length - input.length + 1).join(string) + input;

Usage: strPad ('1', 3, 0);
Output: 001

Disable text selection with CSS

Cross browser CSS to correctly disable user selection of elements.

-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

Although you should not need to interfere with normal selection behavior or conflict with a user's expectations when it comes to text selection, there are a few scenarios in which this property. For example:

  • Navigation buttons in which you do not want the user to accidentally select the text of the button instead of clicking it. (Some browsers e.g. Safari actually let you select the text of normal buttons).
  • This may be necessary for embedded devices. i.e. a device where a browser is used for rendering the UI.
  • There are also legal issues where 3rd party content is being legally republished but a clause in the license requires web publishers to prevent text from being easily copied and pasted.

A JavaScript option should be avoided due to the inconsistencies of browsers.

jQuery Get + Post returned headers

This snippet allows you to use the response headers from a $.get and $.post function.

Particularly useful if you return a PHP response with additional information for jQuery to handle. i.e: Custom Header: Could not connect to database.

$(function() {

	var result = $.get('index.php', function(data) {

		console.log ( result.getAllResponseHeaders() );
		console.log ( result.getResponseHeader('Date') );




Date: Tue, 18 Sep 2012 11:24:40 GMT
Content-Encoding: gzip
X-Powered-By: PHP/5.3.5
Connection: Keep-Alive
Content-Length: 2713
Server: Apache/2.2.17 (Win32) PHP/5.3.5
Vary: Accept-Encoding
Content-Type: text/html
Keep-Alive: timeout=5, max=98
Tue, 18 Sep 2012 11:24:40 GMT