Archive for Uncategorized

Loading Scripts without Blocking

function loadScript(url, callback){

var script = document.createElement("script")
script.type = "text/javascript";

if (script.readyState){  //IE
script.onreadystatechange = function(){
if (script.readyState == "loaded" ||
script.readyState == "complete"){
script.onreadystatechange = null;
callback();
}
};
} else {  //Others
script.onload = function(){
callback();
};
}

script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}

source

AS2 Banner with ClickTag Template

// see the source files found in the zip in the URL above.
// Thanks to Nate63179 for the idea of ClickTag URL rollover
// <a href="http://snipplr.com/view/14239/actionscript-20-click-tag-handler/

" >http://snipplr.com/view/14239/actionscript-20-click-tag-handler/
source

recursively walks a Cakephp threaded array (Tree behavior)

// ESTO ES EN EL MODELO

// CUSTOM VARS
var $documentsList = array(); // esta propiedad guardará lo que vamos tomando del array

// CUSTOM METHODS

// devuelve un listado de los elementos del arbol con sus elementos principales
function getDocumentsList() {

// creo el array que contendrá los datos
$documentList = array();

// traigo los datos
$documents = $this->find('threaded', array('order' => 'Document.folder DESC, Document.description ASC') );
//pr( $documents );

// creo una variable global que pueda ser accedida por la otra función para ir guardando los valores
$this->traverseArray( $documents );

return( $this->documentsList );

}

// recorre el array anidado y va asignando en el array los valores
function traverseArray( $items, $level = -1 ) {

$level++;

foreach( $items as $item ) {

// datos de cada elemento
$this->documentsList[] = array( 'id' => $item['Document']['id'], 'text' => $item['Document']['description'], 'level' => $level, 'folder' => $item['Document']['folder'], 'filename' => $item['Document']['filename'] );

// si es un array y no está vacío lo recorre
if ( is_array( $item['children'] ) && !empty( $item['children'] ) ) {
$this->traverseArray( $item['children'], $level );
}
}

}

source

Generate Insert Statements

USE AdventureWorks
GO
DECLARE @Columns VARCHAR(max);
SET @Columns = '[CultureID], [Name], [ModifiedDate]'
DECLARE @Table VARCHAR(max);
SET @Table = 'Production.Culture'

DECLARE @SQL VARCHAR(max)
SET @SQL = 'DECLARE @S VARCHAR(MAX)
SELECT @S = ISNULL(@S + '' UNION '',
''INSERT INTO ' + @Table + '(' + @Columns + ')'')
+ CHAR(13) + CHAR(10)
+ ''SELECT '' + ' + REPLACE(REPLACE(REPLACE(@Columns, ',', ' + '', '' + '),
'[', ''''''''' + CAST('),']',' AS VARCHAR(max)) + ''''''''')
+' FROM ' + @Table
+ ' PRINT @S'

EXEC (@SQL)

source

imapsync script to migrate Gmail to Google Apps

#!/bin/bash LOGFILE=imapsync.log FOLDERS=( 'Folder1' 'Folder2' 'Folder3' 'Folder4' '...' 'FolderN' 'INBOX' 'All Mail' 'Bin' 'Drafts' 'Starred' 'Sent Mail' ) TIMES=( '--minage 1090' '--maxage 1091 --minage 999' '--maxage 1000 --minage 908' '--maxage 909 --minage 817' '--maxage 818 --minage 726' '--maxage 727 --minage 635' '--maxage 636 --minage 544' '--maxage 545 --minage 453' '--maxage 454 --minage 362' '--maxage 363 --minage 271' '--maxage 272 --minage 180' '--maxage 181 --minage 89' '--maxage 90' ) echo "Starting" > $LOGFILE for FOLDER in "$@" do for ((j=0;j<${#TIMES};j++)) do TIME=${TIMES[${j}]} echo "" >> $LOGFILE echo "*** $FOLDER $TIME ***" >> $LOGFILE echo "" >> $LOGFILE while ! ~/imapsync-1.267/imapsync --host1 imap.gmail.com  --port1 993 --user1 <a href="mailto:user@gmail.com">user@gmail.com</a>  --passfile1 ./passfile --ssl1  --host2 imap.gmail.com  --port2 993 --user2 <a href="mailto:user@domain.com">user@domain.com</a>  --passfile2 ./passfile --ssl2  --syncinternaldates --split1 100 --split2 100  --authmech1 LOGIN --authmech2 LOGIN  --include "$FOLDER"  $TIME  --useheader "Message-ID"  --useheader "Date" --skipsize  --regexmess 's/Delivered-To: <a href="mailto:user@gmail.com">user@gmail.com</a>/Delivered-To: <a href="mailto:user@domain.com">user@domain.com</a>/gi'  --regexmess 's/<user@gmail.com>/<user@domain.com>/gi'  --regexmess 's/^((To|From|Cc|Bcc):.*)user@gmail.com(.*)$/$1user@domain.com$3/gim'  --regexmess 's/Subject:(s*)
/Subject: (no--subject)$1
/ig'  --regexmess 's/Subject: ([Rr][Ee]):(s*)
/Subject: $1: (no--subject)$2
/gi' >> $LOGFILE 2>&1; do echo "" >> $LOGFILE echo "***** NOT COMPLETE - $FOLDER $TIME *****" >> $LOGFILE echo "" >> $LOGFILE tail -100 $LOGFILE | mail -s "Imapsync Restarting for $FOLDER $TIME" "user@domain.com" echo -n "Sleeping..." >> $LOGFILE sleep 1m echo "Done." >> $LOGFILE done echo "" >> $LOGFILE echo "***** COMPLETE - $FOLDER $TIME*****" >> $LOGFILE echo "" >> $LOGFILE tail -100 $LOGFILE | mail -s "Imapsync Complete for $FOLDER $TIME" "user@domain.com" done done echo "**** DONE ****" >> $LOGFILE tail -100 $LOGFILE | mail -s "Imapsync Complete" -c "user@domain.com"

source

Creating a new branch in SVN

svn copy <a href="file:///relative/path/to/trunk/" >file:///relative/path/to/trunk/</a> <a href="file:///relative/path/to/branch/<BRANCH" >file:///relative/path/to/branch/<BRANCH</a> NAME> -m "SVN MESSAGE"

source

Simple JQUERY AJAX POST

<script type="text/javascript">
$(document).ready(function(){
$("form#formId").submit(function() {
inputField = $('#inputFieldId').attr('value');
$.ajax({
type: "POST",
url: "yourpage.php",
cache: false,
data: "inputField ="+ inputField,
success: function(html){
$("#ajax-results").html(html);
}
});
return false;
});
});
</script>

source

Print all XML elements, where given attribute has certain value

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="linCorpus/text/body/div[@id='d923' or @id='d1168' or @id='d1748']">
<xsl:copy-of select="." />
</xsl:template>

<xsl:template match="text()">
</xsl:template>

</xsl:stylesheet>

source

FirePHP example

// start ouput buffering
ob_start();

// be sure you have uploaded the FirePHP lib on your server
require_once('lib/FirePHPCore/FirePHP.class.php');

// instantiate firephp, false would have turned logging off
$firephp = FirePHP::getInstance(true);

// See some more examples at:
// <a href="http://www.firephp.org/HQ/Use.html" >http://www.firephp.org/HQ/Use.html</a>
$firephp->log('Message','Bob was here');

source

PHP getNumberSelect

// FUNCTION: getNumberSelect
//
// create a select box for selecting number from min - max.
//
// $name -> name of the form element, also the id for css selecting.
// $min -> the starting number
// $max -> the end number
// $default -> the default that you want to be selected when shown.
//
//  ex,   getNumberSelect( "birthdate_day" , 1 , 31 , 1 )
//
function getNumberSelect( $name , $min , $max , $default = null )
{
$temp = "";

// if it is not set from the form then show this one as selcted...
if( ! isset( $_POST[$name] ) )
{
$_POST[$name] = intval( $default );
}

// the id is set for styling feel free to mod
$temp .= "<select name="".$name."" id="".$name."" >
";

// we go from min to max, yes yes yes min should be < max
// what fool would call it switched?
while( $min <= $max )
{
$temp .= "<option value="".$min.""";

if( intval( $_POST[$name] ) == intval( $min ) )
{
$temp .= " SELECTED ";
}

// I always put the spaces there cause all the browsers do weird things
// with select boxes and this seemed to make things ok.
$temp .= ">".$min."&nbsp;&nbsp;</option>
";
$min++;
}
$temp .= "</select>
";
return $temp;
}

source