/** * Typewriter style output to screen. * * Usage: * $('#element').typeThis(html,ondone); * * Parameters: * html -- HTML text to type * or object of options that includes a html member. * ondone -- function to execute when typing is done * or URL to redirect the browser to when done. * * Version 3.0 * Copyright 2004,2011, Mack Pexton, mackpexton.com * License http://www.opensource.org/licenses/mit-license.php */ (function($){ var default_options = { html : '', // html to type ondone : null, // url to redirect to or function to call after typing complete timing: [ // milli-second pauses before and after characters //regexp, before after [ /[\w,:]/, 50, 50 ], // regular characters [ /&.{2,7};/, 50, 50 ], // html entities [ / /, 50, 300 ], // space character (pause between words) [ /\W/, 750, 50 ], // punctuation [ /./, 50, 50 ] // everything else ], ondone_pause: 1500, // pause before invoking function/url when done }; $.fn.typeThis = function(arg,ondone) { var opt = $.extend({},default_options,( (arg == null) ? {} : (typeof arg == 'string') ? { 'html' : arg } : (typeof arg == 'object') ? arg : {} ),( (ondone == null) ? {} : (typeof ondone == 'function') ? { 'ondone' : ondone } : (typeof ondone == 'string') ? { 'ondone' : function(){location.href = ondone;} } : {} )), $this = $(this), scurr = $this.html(), // initialize displayed string sarr = opt['html'].split(''), // make array of individual characters index = 0, // initialize counter beforeChar=1, afterChar=2, state=afterChar, html_tags = function() { // return html tags var i = index, l = sarr.length, c, s = '', j, lookahead; TAG:while (i < l) { c = sarr[i]; if (c != '<') break; // Look for tag closing bracket. lookahead = c; for (j=i+1; j') { s += lookahead; i = j + 1; continue TAG; } } // End bracket of tag not found. break; } index = i; // readjust global index to sarr array return s; }, typer = function() { var i, l, c, s; if (state == beforeChar) { state = afterChar; // next state // Search timing array for time to wait before displaying next character. c = sarr[index-1]; for (i=0,l=opt['timing'].length; i