Skip to content

Instantly share code, notes, and snippets.

Avatar

Sindre Sorhus sindresorhus

View GitHub Profile
@sindresorhus
sindresorhus / jquery.toggletext.js
Created Aug 12, 2010
jQuery plugin - Toggle between the current and supplied text
View jquery.toggletext.js
$.fn.toggleText = function(b) {
return this.each(function() {
var $this = $(this);
if ( !$this.data('text') ) {
$this.data( 'text', $this.text() );
}
$this.text( $this.text() !== b ? b : $this.data('text') );
});
};
@sindresorhus
sindresorhus / jquery.hoverclasslive.js
Created Aug 13, 2010
jQuery plugin - Add hover in IE6 on live elements
View jquery.hoverclasslive.js
$.fn.hoverClassLive = function(str) {
var klass = str || 'hover';
return this.live('hover', function() {
$(this).toggleClass(klass);
});
};
@sindresorhus
sindresorhus / jquery.random.js
Created Sep 24, 2010
jQuery plugin - Returns a random element
View jquery.random.js
$.fn.random = function() {
var rand = Math.floor( Math.random() * this.length + 1 );
return this[rand];
};
@sindresorhus
sindresorhus / mysql-backup-windows.bat
Created Mar 14, 2011
Backup MySQL databases in separate gzipped sql files on Windows
View mysql-backup-windows.bat
@echo off
set dbUser=root
set dbPassword=password
set backupDir="C:\Documents and Settings\user\Desktop\backup\mysql"
set mysqldump="C:\Program Files\MySQL\MySQL Workbench 5.2 CE\mysqldump.exe"
set mysqlDataDir="C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.1\data"
set zip="C:\Program Files\7-Zip\7z.exe"
:: get date
@sindresorhus
sindresorhus / jquery.samesize.js
Created Apr 24, 2011
jQuery plugin - Equalize the elements to the same size - width or height.
View jquery.samesize.js
$.fn.sameSize = function( width, max ) {
var prop = width ? 'width' : 'height',
size = Math.max.apply( null, $.map( this, function( elem ) {
return $( elem )[ prop ]();
})),
max = size < max ? size : max;
return this[ prop ]( max || size );
};
// same height
@sindresorhus
sindresorhus / init.py
Created Aug 28, 2011
Boxee - Picture player
View init.py
import mc
mc.ActivateWindow(14000)
item_list = mc.ListItems()
item = mc.ListItem(mc.ListItem.MEDIA_PICTURE)
item.SetPath('http://sindresorhus.com/slider-img/1.jpg')
item.SetImage(0, 'http://sindresorhus.com/slider-img/1.jpg')
item.SetContentType('image/jpeg')
@sindresorhus
sindresorhus / jquery.touchhover.js
Created Oct 11, 2011
jQuery plugin - Adds a .hover class on click on touch devices. Which will make it easier to use :hover and still support touch devices.
View jquery.touchhover.js
$.fn.touchHover = function() {
return 'ontouchstart' in document.documentElement ? this.click(function() {
$(this).toggleClass('hover');
}) : this;
};
/*
Example: $('.button').touchHover();
You also need to add .hover to your :hover CSS rules:
.button:hover -> .button:hover, .button.hover
*/
@sindresorhus
sindresorhus / change_form.html
Created Oct 14, 2011
Django - If you have 2 date-pickers in the admin form, one for start date and one for end date, this will make sure the second picker is the same month as the first. Which improves the usability of the picker.
View change_form.html
{% extends "admin/change_form.html" %}
{% block extrahead %}
{{ block.super }}
<script >
// override openCalendar to set the date of calendar A to calendar B
var _openCalendar = DateTimeShortcuts.openCalendar;
DateTimeShortcuts.openCalendar = function(num) {
_openCalendar.call( this, num );
var cals = DateTimeShortcuts.calendars;
cals[1].drawDate( cals[0].currentMonth, cals[0].currentYear );
@sindresorhus
sindresorhus / countrycode-latlong-array.json
Created Nov 5, 2011
Country codes (ISO 3166) to latitude longitude - converted from http://www.maxmind.com/app/country_latlon
View countrycode-latlong-array.json
{
"ad": [
"42.5000",
"1.5000"
],
"ae": [
"24.0000",
"54.0000"
],
"af": [
@sindresorhus
sindresorhus / simplexhr.js
Created Jan 9, 2012
Simple XMLHttpRequest wrapper
View simplexhr.js
var xhr = function() {
var xhr = new XMLHttpRequest();
return function( method, url, callback ) {
xhr.onreadystatechange = function() {
if ( xhr.readyState === 4 ) {
callback( xhr.responseText );
}
};
xhr.open( method, url );
xhr.send();