JS Web Chat

An example of using the HTML5 Dialog Element in a chat screen.

by Roberto Apasajja

HTML

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Web Chat</title>
    </head>
    <body>
        <div class="page">        
            <section id="main">
                <div id="chatLog"></div>
            </section>
            <header style="position: fixed; left: 10px; top: 0px;">
                <hgroup>
                    <h1>Chattin' it up</h1>            
                </hgroup>
            </header>
            <footer style="position: fixed; left: 10px; bottom: 0;">
                <a class="botToggle" href="#" onclick="toggleBot()">Toggle Bot On/Off</a>
                <textarea class="chatText" placeholder="Type a Message" autofocus></textarea><br />
                <input id="submitBtn" type="submit" value="Send" />
            </footer>
            <div id="typingIndicator" style="display: none;">Typing a Message</div>
        </div>
    </body>
</html>

CSS

/**
* HTML5 ✰ Boilerplate
*
* style.css contains a reset, font normalization and some base styles.
*
* Credit is left where credit is due.
* Much inspiration was taken from these projects:
* - yui.yahooapis.com/2.8.1/build/base/base.css
* - camendesign.com/design/
* - praegnanz.de/weblog/htmlcssjs-kickstart
*/
/**
* html5doctor.com Reset Stylesheet (Eric Meyer's Reset Reloaded + HTML5 baseline)
* v1.6.1 2010-09-17 | Authors: Eric Meyer & Richard Clark
* html5doctor.com/html-5-reset-stylesheet/
*/

html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code, del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var, b, i, dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}

article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
    display: block;
}

blockquote, q { quotes: none; }

blockquote:before, blockquote:after,
q:before, q:after { content: ''; content: none; }

ins { background-color: #ff9; color: #000; text-decoration: none; }

mark { background-color: #ff9; color: #000; font-style: italic; font-weight: bold; }

del { text-decoration: line-through; }

abbr[title], dfn[title] { border-bottom: 1px dotted; cursor: help; }

table { border-collapse: collapse; border-spacing: 0; }

hr { display: block; height: 1px; border: 0; border-top: 1px solid #ccc; margin: 1em 0; padding: 0; }

input, select { vertical-align: middle; }


/**
* Font normalization inspired by YUI Library's fonts.css: developer.yahoo.com/yui/
*/

body { font:13px/1.231 sans-serif; *font-size:small; } /* Hack retained to preserve specificity. */
select, input, textarea, button { font:99% sans-serif; }

/*...

JavaScript

/*
Author: Jacob Gable, May 2011.
License: Ms-PL; No Attribution Required; Other licenses retained where applicable...
*/

// Do this first so we can minimize funky textbox display;
function updateWidths() {
    // page width - 10 for left offset;
    var newWidth = $('.page').width() - 10;
    $('footer, header').width(newWidth);
};
updateWidths();

// hack for fixing the width when resizing the browser.
$(window).resize(function() {
    updateWidths();
});

// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function() {
    log.history = log.history || []; // store logs to an array for reference
    log.history.push(arguments);
    if (this.console) console.log(Array.prototype.slice.call(arguments));
};

// Placeholder support for IE9
if (!('placeholder' in document.createElement('input'))) {
    $('[placeholder]').each(function() {
        var self = $(this);
        var p = 'placeholder';
        self.data('empty', self.val() == '');
        self.focus(function() {
            if (self.data('empty') && self.val() == self.attr(p)) {
                self.val('').removeClass(p);
            }
        }).blur(function() {
            if (self.data('empty')) {
                self.val(self.attr(p)).addClass(p);
            }
        }).change(function() {
            self.data('empty', self.val() == '');
        });
        if (self.val() == '' || self.val() == self.attr(p)) {
            self.val(self.attr(p)).addClass(p);
        }
    });
    $('form:has([placeholder])').bind('submit', function() {
        $(this).find('[placeholder]').each(function() {
            if ($(this).data('empty')) {
                $(this).val('');
            }
        });
    });
}

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 3/9/2009
 * @author...