jQuery indexOf (9-Feb @ 00:11)
Syntax Highlighted Code
- $(el).prevAll().length;
Plain Code
$(el).prevAll().length;
not in selector (14-Jan @ 00:11)
Syntax Highlighted Code
- jQuery.extend(jQuery.expr[":"], {
- notin: function (a, b, m) {
- return !!!jQuery(a).parents(m[3]).length;
- }
- });
Plain Code
jQuery.extend(jQuery.expr[":"], {
notin: function (a, b, m) {
return !!!jQuery(a).parents(m[3]).length;
}
});
Filter where parent isn't (13-Jan @ 16:22)
Syntax Highlighted Code
- $('tag').parents(':not(selector)').find('> tag')
Plain Code
$('tag').parents(':not(selector)').find('> tag')
Email JS Validation (1-Jan @ 13:04)
Syntax Highlighted Code
- if (!/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/.test(email)) {
- // email is not valid
- }
Plain Code
if (!/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/.test(email)) {
// email is not valid
}
Window Scroll Position (30-Dec @ 13:12)
Syntax Highlighted Code
- function getScrollXY() {
- var scrOfX = 0, scrOfY = 0;
- if( typeof( window.pageYOffset ) == 'number' ) {
- //Netscape compliant
- [12 more lines...]
Plain Code
function getScrollXY() {
var scrOfX = 0, scrOfY = 0;
if( typeof( window.pageYOffset ) == 'number' ) {
//Netscape compliant
scrOfY = window.pageYOffset;
scrOfX = window.pageXOffset;
} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
//DOM compliant
scrOfY = document.body.scrollTop;
scrOfX = document.body.scrollLeft;
} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
//IE6 standards compliant mode
scrOfY = document.documentElement.scrollTop;
scrOfX = document.documentElement.scrollLeft;
}
return [ scrOfX, scrOfY ];
}
Viewport (30-Dec @ 12:43)
Syntax Highlighted Code
- function getViewportHeight() {
- var height = self.innerHeight; // Safari, Opera
- var mode = document.compatMode;
- [20 more lines...]
Plain Code
function getViewportHeight() {
var height = self.innerHeight; // Safari, Opera
var mode = document.compatMode;
if ( (mode || $.browser.msie) && !$.browser.opera ) { // IE, Gecko
height = (mode == 'CSS1Compat') ?
document.documentElement.clientHeight : // Standards
document.body.clientHeight; // Quirks
}
return height;
}
function getViewportWidth() {
var width = self.innerWidth; // Safari, Opera
var mode = document.compatMode;
if ( (mode || $.browser.msie) && !$.browser.opera ) { // IE, Gecko
width = (mode == 'CSS1Compat') ?
document.documentElement.clientWidth : // Standards
document.body.clientWidth; // Quirks
}
return width;
}
encode (1-Dec @ 23:39)
Syntax Highlighted Code
- function encode(str) {
- var s = '';
- for (var i = 0; i < str.length; i++) {
- s += '&#' + str.charCodeAt(i) + ';';
- [2 more lines...]
Plain Code
function encode(str) {
var s = '';
for (var i = 0; i < str.length; i++) {
s += '&#' + str.charCodeAt(i) + ';';
}
return s;
}
repeat (15-Oct @ 01:14)
Syntax Highlighted Code
- String.prototype.repeat = function( num ) {
- return new Array( num + 1 ).join( this );
- }
- alert( "string to repeat\n".repeat( 4 ) );
Plain Code
String.prototype.repeat = function( num ) {
return new Array( num + 1 ).join( this );
}
alert( "string to repeat\n".repeat( 4 ) );
radioChange (10-Sep @ 16:12)
Syntax Highlighted Code
- $.fn.radioChange = function (fn) {
- return this.each(function () {
- if (!$.browser.msie) {
- $(this).change(fn);
- [6 more lines...]
Plain Code
$.fn.radioChange = function (fn) {
return this.each(function () {
if (!$.browser.msie) {
$(this).change(fn);
} else {
$(this).click(fn);
}
});
};
Untitled JavaScript (10-Sep @ 09:35)
Syntax Highlighted Code
- $('div.demo marquee').marquee('pointer').mouseover(function () {
- $(this).trigger('stop');
- }).mouseout(function () {
- $(this).trigger('start');
- [10 more lines...]
Plain Code
$('div.demo marquee').marquee('pointer').mouseover(function () {
$(this).trigger('stop');
}).mouseout(function () {
$(this).trigger('start');
}).mousemove(function (event) {
if ($(this).data('drag') == true) {
this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
}
}).mousedown(function (event) {
$(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
}).mouseup(function () {
$(this).data('drag', false);
});
XSLT range (4-Sep @ 23:27)
Syntax Highlighted Code
- <xsl:template match="b">
- <xsl:for-each select="item[position()>=3 and 7>=position()]" >
Plain Code
<xsl:template match="b">
<xsl:for-each select="item[position()>=3 and 7>=position()]" >
Untitled JavaScript (21-Aug @ 09:26)
Syntax Highlighted Code
- var results = document.querySelectorAll( selector );
- var length = results.length;
- results.constructor = jQuery;
- results.__proto__ = jQuery.prototype;
- results.length = length;
Plain Code
var results = document.querySelectorAll( selector );
var length = results.length;
results.constructor = jQuery;
results.__proto__ = jQuery.prototype;
results.length = length;
CSS Keyframe example (12-Aug @ 12:27)
Syntax Highlighted Code
- .divSlide {
- -webkit-animation-name: "slide-me-to-the-right";
- -webkit-animation-duration: 1s;
- }
- [5 more lines...]
Plain Code
.divSlide {
-webkit-animation-name: "slide-me-to-the-right";
-webkit-animation-duration: 1s;
}
@-webkit-keyframes "slide-me-to-the-right" {
from { left: 0px; }
to { left: 100px; }
}
Untitled JavaScript (11-Aug @ 14:06)
Syntax Highlighted Code
- // navigational helper function.
- goTo : function(where){
- },
- [8 more lines...]
Plain Code
// navigational helper function.
goTo : function(where){
},
customValidationDisplay: function(errorMap, errorList, successList){
function generateTooltip(err) {
$(err.element).attr('title', err.message).tooltip({
delay: 0,
showURL: false,
Block rotator (10-Jul @ 16:28)
Syntax Highlighted Code
- $.fn.rotate = function (delay) {
- var rotators = this,
- n = this.length,
- current = 0,
- [10 more lines...]
Plain Code
$.fn.rotate = function (delay) {
var rotators = this,
n = this.length,
current = 0,
timer = null;
rotators.hide().eq(current).show();
timer = setInterval(function () {
current++;
if (current == n) current = 0;
rotators.hide().eq(current).show();
}, delay);
return rotators;
};
Editable (8-Jul @ 10:45)
Syntax Highlighted Code
- javascript:document.body.contentEditable='true'; document.designMode='on'; void 0;
Plain Code
javascript:document.body.contentEditable='true'; document.designMode='on'; void 0;
addEvent (26-Jun @ 23:36)
Syntax Highlighted Code
- addEvent = (function () {
- return document.body.addEventListener ? function (e, ev, fn) {
- e.addEventListener(ev, fn, false);
- } : function () {
- [2 more lines...]
Plain Code
addEvent = (function () {
return document.body.addEventListener ? function (e, ev, fn) {
e.addEventListener(ev, fn, false);
} : function () {
e.attachEvent("on" + ev, fn);
};
})();
DateJS throttling (21-Jun @ 13:46)
Syntax Highlighted Code
- function delay(fn, time) {
- return function () {
- var t = this;
- [27 more lines...]
Plain Code
function delay(fn, time) {
return function () {
var t = this;
if (fn.timer) {
clearTimeout(fn.timer);
}
fn.timer = setTimeout(function () {
fn.apply(t);
}, time);
};
}
$(function () {
var humanDate = $('#human_date').keyup(delay(function () {
if (humanDate.data('prevValue') == humanDate.val()) return;
humanDate.data('prevValue', humanDate.val());
var date = '';
try {
date = Date.parse(this.value).toString("d-MMMM yyyy"); // note different formats available
} catch (e) {
date = 'Sorry, I cant do that date';
}
$('#info').html(''); // error notice field
$('#actualDate').text(date); // date selected (i.e. in plain text)
}, 200));
// force a trigger
humanDate.keyup();
});
Untitled HTML (19-Jun @ 13:03)
Syntax Highlighted Code
Plain Code
<img class="plain floatRight" alt="Telefono" src="/images/telefono.gif"/>
<p style="margin-top: 22px;"> Per saperne di pi </p>
Untitled JavaScript (16-Jun @ 12:29)
Syntax Highlighted Code
- $(function () {
- var tabs = [];
- var tabContainers = [];
- $('ul.tabs a').each(function () {
- [21 more lines...]
Plain Code
$(function () {
var tabs = [];
var tabContainers = [];
$('ul.tabs a').each(function () {
// note that this only compares the pathname, not the entire url
// which actually may be required for a more terse solution.
if (this.pathname == window.location.pathname) {
tabs.push(this);
tabContainers.push($(this.hash).get(0));
}
});
// sniff for hash in url, and create filter search
var selected = window.location.hash ? '[hash=' + window.location.hash + ']' : ':first';
$(tabs).click(function () {
// hide all tabs
$(tabContainers).hide().filter(this.hash).show();
// set up the selected class
$(tabs).removeClass('selected');
$(this).addClass('selected');
return false;
}).filter(selected).click();
});
Untitled JavaScript (13-Jun @ 17:33)
Syntax Highlighted Code
- $.fn.codaSlider = function (options, scrollOptions) {
- var defaults = {
- horizontal : true,
- // etc
- [13 more lines...]
Plain Code
$.fn.codaSlider = function (options, scrollOptions) {
var defaults = {
horizontal : true,
// etc
navigation : '.navigation a' // required
};
var settings = $.extend({}, defaults, options);
var ret = this.each(function () {
// do coda business
};
$.localScroll(scrollSettings); // where does scrollSettings derive from?
return ret;
}
center align (9-Jun @ 09:51)
Syntax Highlighted Code
- .center {
- display: block;
- margin: 0 auto;
- }
Plain Code
.center {
display: block;
margin: 0 auto;
}
Code Igniter - load a model from a model (7-Jun @ 19:11)
Syntax Highlighted Code
- // from within your model
- $CI =& get_instance();
- $CI->load->model('other');
- [1 more lines...]
Plain Code
// from within your model
$CI =& get_instance();
$CI->load->model('other');
$event = $CI->Other->method($var);
Sample hCalendar (28-May @ 11:26)
Syntax Highlighted Code
Plain Code
<dl class="vevent">
<dt>Game</dt>
<dd class="summary">Grand Theft Auto: Liberty City Stories (PSP)</dd>
<dt>Where</dt>
<dd><span class="location">Work</span>, <abbr title="2008-05-28T12:30:00-00:00">Wednesday 28th May @ 12:30PM GMT</abbr></dd>
<dt>Type</dt>
<dd>Multiplayer</dd>
<dt>Added by</dt>
<dd><a href="/people/chrismahon">Chrismahon</a></dd>
</dl>
Arguments to array (28-May @ 00:54)
Syntax Highlighted Code
- var args = Array.prototype.slice.apply(arguments);
Plain Code
var args = Array.prototype.slice.apply(arguments);
Untitled Diff (26-May @ 21:26)
Syntax Highlighted Code
- Index: /Users/remy/Sites/test.com/htdocs/jqueryjs/jquery/src/selector.js
- ===================================================================
- --- /Users/remy/Sites/test.com/htdocs/jqueryjs/jquery/src/selector.js (revision 5696)
- +++ /Users/remy/Sites/test.com/htdocs/jqueryjs/jquery/src/selector.js (working copy)
- [10 more lines...]
Plain Code
Index: /Users/remy/Sites/test.com/htdocs/jqueryjs/jquery/src/selector.js
===================================================================
--- /Users/remy/Sites/test.com/htdocs/jqueryjs/jquery/src/selector.js (revision 5696)
+++ /Users/remy/Sites/test.com/htdocs/jqueryjs/jquery/src/selector.js (working copy)
@@ -336,7 +336,7 @@
for ( var i = 0, rl = r.length; i < rl; i++ ) {
var a = r[i], z = a[ jQuery.props[m[2]] || m[2] ];
- if ( z == null || /href|src|selected/.test(m[2]) )
+ if ( z == null || /style|href|src|selected/.test(m[2]) )
z = jQuery.attr(a,m[2]) || '';
if ( (type == "" && !!z ||
getQuery (24-May @ 00:16)
Syntax Highlighted Code
- function getQuery(s) {
- var query = {};
- s.replace(/\b([^&=]*)=([^&=]*)\b/g, function (m, a, d) {
- [9 more lines...]
Plain Code
function getQuery(s) {
var query = {};
s.replace(/\b([^&=]*)=([^&=]*)\b/g, function (m, a, d) {
if (typeof query[a] != 'undefined') {
query[a] += ',' + d;
} else {
query[a] = d;
}
});
return query;
}
Untitled JavaScript (16-May @ 08:26)
Syntax Highlighted Code
- $.ajax({
- 'url' : settings.url,
- 'dataType' : 'json',
- 'data' : { 'tag' : currentTag.tag },
- [7 more lines...]
Plain Code
$.ajax({
'url' : settings.url,
'dataType' : 'json',
'data' : { 'tag' : currentTag.tag },
'async' : false, // wait until this is ajax hit is complete before continue
'success' : function (m) {
matches = m.matches;
}
});
Untitled HTML (10-May @ 12:09)
Syntax Highlighted Code
Plain Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div id="twitters">
<p><img src="/resources/images/ajaxload.gif" /> Connecting to live social updates...</p>
</div>
<script src="/include/scripts/twitter.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
getTwitters('tweet', {
id: 'matbe81',
count: 1,
enableLinks: true,
ignoreReplies: true,
clearContent: true,
template: '<img src="%user_profile_image_url%"/> %text% <a href="http://twitter.com/%user_screen_name%/statuses/%id%/">%time%</a><a href="http://twitter.com/statuses/user_timeline/%user_screen_name%.rss"><img src="/resources/icons/feed-icon-12x12.gif" border="0"/></a>'
});
</script>
</body>
</html>
Untitled JavaScript (28-Apr @ 12:35)
Syntax Highlighted Code
- $(this).parents('form:first');
Plain Code
$(this).parents('form:first');
Untitled JavaScript (25-Apr @ 17:36)
Syntax Highlighted Code
- getTwitters('tweet', {
- id: 'thobu',
- count: 3,
- enableLinks: true,
- [5 more lines...]
Plain Code
getTwitters('tweet', {
id: 'thobu',
count: 3,
enableLinks: true,
ignoreReplies: true,
clearContents: true,
template: '"%text%" <a href="http://twitter.com/%user_screen_name%/statuses/%id%/" onclick="javascript:pageTracker._trackPageview' +
"('/outbound/article/http://twitter.com/%user_screen_name%/statuses/%id%/');" +
'">%time%</a>'
});
Generate dummy file (22-Apr @ 11:42)
Syntax Highlighted Code
- dd if=/dev/zero of=dummy.file bs=1000000 count=1
Plain Code
dd if=/dev/zero of=dummy.file bs=1000000 count=1
jQuery.requires (18-Apr @ 10:50)
Syntax Highlighted Code
- jQuery.extend({
- compareVersions : function (have, require) {
- if (typeof require == 'undefined') {
- require = have;
- [34 more lines...]
Plain Code
jQuery.extend({
compareVersions : function (have, require) {
if (typeof require == 'undefined') {
require = have;
have = jQuery.fn.jquery;
}
function parts(val) {
// Expecting following format 1.2.3rc1, 1.2.4a, etc.
return val.replace(/[a-z]+/i, function(m) {
return '.' + (m == 'pre' ? '0pre' : m);
}).split('.');
}
if (require == have) return 0;
var latest = parts(have),
required = parts(require),
i = 0;
while (latest[i] && required[i]) {
if (latest[i] < required[i]) {
return -1;
} else if (latest[i] > required[i]) {
return 1;
}
i++;
}
return (latest[i]
? (/^d/.test(latest[i]) ? 1 : -1)
: (/^d/.test(required[i]) ? -1 : 1));
},
hasVersion : function() {
var ok = this.compareVersions.apply(this, arguments);
return (ok === 0 || ok === 1);
}
});
Untitled JavaScript (16-Apr @ 10:28)
Syntax Highlighted Code
- $(function () {
- var max = 8;
- var min = 1;
- var cur = 5;
- [44 more lines...]
Plain Code
$(function () {
var max = 8;
var min = 1;
var cur = 5;
var up = $('#up').click(function () {
var moved = false;
if (cur != min) {
cur--;
moved = true;
}
console.log(cur);
$('ul').trigger('redraw', [moved]);
return false;
});
var down = $('#down').click(function () {
var moved = false;
if (cur != max) {
cur++;
moved = true;
}
console.log(cur);
$('ul').trigger('redraw', [moved]);
return false;
});
$('ul').bind('redraw', function (moved) {
// helps us optimise
if (moved) {
if (cur == min) {
up.html('up - disabled');
} else {
up.html('up');
}
if (cur == max) {
down.html('down - disabled');
} else {
down.html('down');
}
$('li', this).slice(0, cur).show().end().slice(cur).hide();
}
}).trigger('redraw', [true]);
});
sleep (15-Apr @ 07:55)
Syntax Highlighted Code
- function sleep(milliseconds) {
- var start = new Date().getTime();
- for (var i = 0; i < 1e7; i++) {
- if ((new Date().getTime() - start) > milliseconds){
- [4 more lines...]
Plain Code
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
Password Generator (14-Apr @ 11:48)
Syntax Highlighted Code
- function generatePassword(limit, inclNumbers) {
- var vowels = 'aeiou'.split('');
- var constonants = 'bcdfghjklmnpqrstvwxyz'.split('');
- var word = '', i, num;
- [19 more lines...]
Plain Code
function generatePassword(limit, inclNumbers) {
var vowels = 'aeiou'.split('');
var constonants = 'bcdfghjklmnpqrstvwxyz'.split('');
var word = '', i, num;
if (!limit) limit = 8;
for (i = 0; i < (inclNumbers ? limit - 3 : limit); i++) {
if (i % 2 == 0) { // even = vowels
word += vowels[Math.floor(Math.random() * 4)];
} else {
word += constonants[Math.floor(Math.random() * 20)];
}
}
if (inclNumbers) {
num = Math.floor(Math.random() * 99) + '';
if (num.length == 1) num = '00' + num;
else if (num.length == 2) num = '0' + num;
word += num;
}
return word.substr(0, limit);
}
Password Generator (14-Apr @ 11:13)
Syntax Highlighted Code
Plain Code
function GeneratePassword($limit = 8) {
$vowels = array('a', 'e', 'i', 'o', 'u');
$const = array('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z');
$word = '';
for ($i = 0; $i < ($limit - 3); $i++) {
if ($i % 2 == 0) { // even = vowels
$word .= $vowels[rand(0, 4)];
} else {
$word .= $const[rand(0, 20)];
}
}
$num = rand(0,999);
str_pad($num, 3, '0', STR_PAD_LEFT);
return substr($word . $num, 0, $limit);
}
JavaScript namespace (12-Apr @ 12:49)
Syntax Highlighted Code
- String.prototype.namespace = function(separator) {
- var ns = this.split(separator || '.'), p = window;
- for (i = 0; i < ns.length; i++) {
- p = p[ns[i]] = p[ns[i]] || {};
- [1 more lines...]
Plain Code
String.prototype.namespace = function(separator) {
var ns = this.split(separator || '.'), p = window;
for (i = 0; i < ns.length; i++) {
p = p[ns[i]] = p[ns[i]] || {};
}
};
Untitled Bash (12-Apr @ 12:48)
Syntax Highlighted Code
- mysql $DB -u$USERNAME -p$PASSWORD -e 'show tables like "$LIKE%"' |
- grep -v Tables_in |
- xargs mysqldump --add-drop-table $DB -u$USERNAME -p$PASSWORD
Plain Code
mysql $DB -u$USERNAME -p$PASSWORD -e 'show tables like "$LIKE%"' |
grep -v Tables_in |
xargs mysqldump --add-drop-table $DB -u$USERNAME -p$PASSWORD
Detect insertion position (8-Apr @ 16:10)
Syntax Highlighted Code
- (function () {
- function getLastChild(el) {
- return (el.lastChild && el.lastChild.nodeName != '#text') ? getLastChild(el.lastChild) : el;
- }
- [10 more lines...]
Plain Code
(function () {
function getLastChild(el) {
return (el.lastChild && el.lastChild.nodeName != '#text') ? getLastChild(el.lastChild) : el;
}
// should be our script tag
var insertPosition = getLastChild(document.lastChild);
$(document).ready(function () {
// get widget via jsonp
// target element should be create at insertPosition, i.e.
$(insertPosition).after('<p>widget inserted here</p>');
});
})();
Untitled JavaScript (4-Apr @ 15:16)
Plain Code
<h1>Fade Method 1</h1>
<img src="images/who.jpg">
<span style="margin: 0pt;
padding: 0pt;
background: transparent url(images/who_ro.jpg) no-repeat scroll 0%;
height: 183px;
width: 260px;
display: -moz-inline-block;
-moz-background-clip: -moz-initial;
-moz-background-origin: -moz-initial;
-moz-background-inline-policy: -moz-initial;">
<img class="fade" src="images/who.jpg" style="background-color: transparent;
background-repeat: no-repeat;
background-attachment: scroll;
background-position: 0%;
-moz-background-clip: -moz-initial;
-moz-background-origin:
-moz-initial;
-moz-background-inline-policy: -moz-initial;">
</span>
<img src="images/who_ro.jpg">
filterData plugin (3-Apr @ 15:42)
Syntax Highlighted Code
- $.fn.filterData = function (k, v) {
- return this.filter(function () {
- return ($(this).data(k) == v);
- });
- };
Plain Code
$.fn.filterData = function (k, v) {
return this.filter(function () {
return ($(this).data(k) == v);
});
};
Extend to single instance (31-Mar @ 14:07)
Syntax Highlighted Code
- var WFE = {};
- WFE.PanelManager = Base.extend({
- init: function () {
- console.log('parent initialised');
- [17 more lines...]
Plain Code
var WFE = {};
WFE.PanelManager = Base.extend({
init: function () {
console.log('parent initialised');
this.id = 'PanelManager';
}
});
WFE.PanelManager = WFE.PanelManager.extend({
constructor: null, // forced the class in to a single instance http://dean.edwards.name/weblog/2006/03/base/
init: function () {
console.log('child init - testing parent...');
this.base();
}
});
// WFE.PanelManager is now a single object that can be extended as per above
// and it's correctly inherited the properties and methods of it's parent.
console.log(WFE.PanelManager);
On ready (4-Jan @ 17:59)
Syntax Highlighted Code
- $(function () {
- // do stuff
- });
Plain Code
$(function () {
// do stuff
});
Ajax validation plugin (4-Jan @ 17:57)
Syntax Highlighted Code
- (function ($) {
- $.fn.liveCheck = function () {
- return this.each(function () {
- var $$ = $(this);
- [38 more lines...]
Plain Code
(function ($) {
$.fn.liveCheck = function () {
return this.each(function () {
var $$ = $(this);
var t = this;
var url = $$.parents('form:first').attr('action');
var infospan = $$.parent().find('span');
if (!infospan.length) {
infospan = $$.after(' <span></span>').next();
}
$$.keyup(function () {
// prevent tabbing in to the field firing an ajax hit
if (t.value == '' && (t.lastValue == undefined)) return true;
if (t.value != t.lastValue) {
if (this.timer) clearTimeout(this.timer);
infospan.removeClass('error').html('<img src="/images/ajax-loader.gif" height="16" width="16" /> validating...');
this.timer = setTimeout(function () {
$.ajax({
url: url,
data: t.name + '=' + t.value,
dataType: 'json',
type: 'post',
success: function (j) {
infospan.toggleClassIf(!j.ok, 'error').html(j.msg);
}
});
}, 200);
t.lastValue = t.value;
}
});
});
};
$.fn.toggleClassIf = function (v, c) {
return v ? this.addClass(c) : this.removeClass(c);
};
})(jQuery);
Test for Module (14-Dec @ 08:22)
Plain Code
eval {require Module};
unless ($@)
{
require Module;
import Module;
# do stuff with Module
}
Excel file header (5-Dec @ 21:15)
Syntax Highlighted Code
- <?php
- $export_file = "my_name.xls";
- [12 more lines...]
Plain Code
<?php
$export_file = "my_name.xls";
ob_end_clean();
ini_set('zlib.output_compression','Off');
header('Pragma: public');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
header('Pragma: no-cache');
header('Expires: 0');
header('Content-Transfer-Encoding: none');
header('Content-Type: application/vnd.ms-excel;'); // This should work for IE & Opera
header('Content-type: application/x-msexcel'); // This should work for the rest
header('Content-Disposition: attachment; filename="' . basename($export_file) . '"');
?>
MySQL dump tables like (4-Dec @ 19:11)
Syntax Highlighted Code
- mysql $DB -u$USERNAME -p$PASSWORD -e 'show tables like "$LIKE%"' |
- grep -v Tables_in | xargs mysqldump --add-drop-table $DB -u$USERNAME -p$PASSWORD
Plain Code
mysql $DB -u$USERNAME -p$PASSWORD -e 'show tables like "$LIKE%"' |
grep -v Tables_in | xargs mysqldump --add-drop-table $DB -u$USERNAME -p$PASSWORD
CamelCase (22-Nov @ 21:44)
Syntax Highlighted Code
- String.prototype.toCamelCase = function() {
- return this.toString()
- .replace(/([A-Z]+)/g, function(m,l){
- return l.substr(0,1).toUpperCase() + l.toLowerCase().substr(1,l.length);
- [4 more lines...]
Plain Code
String.prototype.toCamelCase = function() {
return this.toString()
.replace(/([A-Z]+)/g, function(m,l){
return l.substr(0,1).toUpperCase() + l.toLowerCase().substr(1,l.length);
})
.replace(/[-_s](.)/g, function(m, l){
return l.toUpperCase();
});
};
Loose DOM parsing (3-Oct @ 12:30)
Syntax Highlighted Code
- $dom = new DOMDocument();
- $dom->preserveWhiteSpace = false;
- $dom->strictErrorChecking = false;
- $dom->loadHTMLFile($url);
Plain Code
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->strictErrorChecking = false;
$dom->loadHTMLFile($url);
base64encode (30-Sep @ 12:30)
Syntax Highlighted Code
- perl -MMIME::Base64 -e' open(FILE, $ARGV[0]) or die "$!";while (read(FILE, $buf, 60*57)) {print encode_base64($buf);}'
Plain Code
perl -MMIME::Base64 -e' open(FILE, $ARGV[0]) or die "$!";while (read(FILE, $buf, 60*57)) {print encode_base64($buf);}'