/** * Typewriter style output to screen. * * Usage: * var t = new Typewriter(element); * t.type(string,ondone); * * Parameters: * string -- HTML string to output * ondone -- function to execute when typing is done * or URL to redirect the browser to when done * * Version 2.1 * Copyright 2004,2010, Mack Pexton, mackpexton.com * License http://www.opensource.org/licenses/mit-license.php */ (function(){ var typewriter = function(e) { this.e = (typeof e == 'string') ? document.getElementById(e) : e; // element where typing goes this.is_done = false; // flag set when done typing } typewriter.prototype = { 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 type: function(str,ondone) { var sarr = str.split(''), // make array of individual characters index = 0, // initialize counter scurr = this.e.innerHTML, // initialize displayed string beforeChar=1, afterChar=2, state=afterChar, timing = this.timing, pause = this.ondone_pause, t = this, 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=timing.length; i