Welcome

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

What next?
1. Bookmark us with del.icio.us or Digg Us!
2. Subscribe to this site's RSS feed
3. Browse the site.
4. Post your own code snippets to the site!

Onload Fix

// Fixes Onload so that script gets executed as soon as the DOM is complete

// Dean Edwards/Matthias Miller/John Resig

function init() {
    // quit if this function has already been called
    if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

    // kill the timer
    if (_timer) clearInterval(_timer);

    // do stuff
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
window.onload = init;

Preventing Apache memory leaks with ulimit


#!/bin/sh

HTTPD=/usr/local/apache2/bin/httpd
CONF=/bmi/httpd-php/conf/httpd.conf
                         
exec 2>&1
echo starting...
ulimit -v 100000
exec $HTTPD -f $CONF -D NO_DETACH

Xml Config Snippet For XMLSerializer Temporary Classes


<system.diagnostics>
  <switches>
    <add name="XmlSerialization.Compilation" value="4"/>
  </switches>
</system.diagnostics>

Trim() Function

// www.codestore.net/store.nsf/unid/BLOG-20060313

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

Change password for mysql root user on debian


UPDATE mysql.user SET Password = PASSWORD('newpwd') WHERE User = 'root';
FLUSH PRIVILEGES;

Backup a remote MySQL database to your local hard disk


ssh user@example.com 'mysqldump -q -u database_user -p database optional_table' > backup.sql

Block Lists


/* -- blocklists --*/
ul.blockList {
	list-style-type: none;
	margin: 20px 0;
}

ul.blockList li {
	margin-bottom: 20px
}
ul.blockList li h3,
ul.blockList li p.meta {
	display: inline;
	margin: 0;
	padding: 0;
}
ul.blockList li p.meta {
	font-size: 0.8em;
	margin-left: 1em;
	margin-top: 0;
}
ul.blockList li blockquote {
	border: none;
	margin: 0.5em 0 0 0;
	padding-left: 0;
	font-style: normal;
}

FB Like Button Scale

// Cross Browser scaling.

.fb-like, fb\:like {
    -webkit-transform: scale(5);
    -moz-transform: scale(5);
    -o-transform: scale(5);
    -ms-transform: scale(5);
    transform: scale(5);
}

ubuntu cron reference

Example: sudo -i crontab -e
reindex locate db daily

0 0 * * * /usr/bin/updatedb


# m h  dom mon dow   command

# MIN HOUR MDAY MON DOW COMMAND

Code:
MIN = Minute 0-60 HOUR = Hour [24-hour clock] 0-23
MDAY = Day of Month 1-31 
MON = Month 1-12 OR jan,feb,mar,apr ... 
DOW = Day of Week 0-6 OR sun,mon,tue,wed,thu,fri,sat COMMAND = Command to be run Any valid command-line

@reboot Run once, at startup. None

@yearly Run once a year 0 0 1 1 *

@annually (same as @yearly) 0 0 1 1 *

@monthly Run once a month 0 0 1 * *

@weekly Run once a week 0 0 * * 0

@daily Run once a day 0 0 * * *

@midnight (same as @daily) 0 0 * * *

@hourly Run once an hour 0 * * * *

svnadd.groovy Add all svn new status files to working directory


#!/usr/bin/env groovy

// Add all new files in working dir into svn
// Usage svnadd.groovy [working_dir]
def wd = args.size()>0 ? args[0] : '.'
def svnStatusCmd = "svn st $wd"
def svnAddCmd = "svn add "

svnStatusCmd.execute().text.split("\n").each{ line ->
       matcher = (line =~ /^\?\s+(.+)$/)
       if(matcher.find()){
               def file = matcher.group(1)
               def cmd = svnAddCmd + " " + file
               print cmd.execute().text
       }
}