forked from patik/console.log-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
64 lines (57 loc) · 1.99 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// The list of sample commands used to demonstrate the log() function
var commands = {
'log-exec1': {
'method': function () { log( "Here's a string" , 3.14, {"three":false} ); }
},
'log-exec2': {
'method': function () { log( function () { alert("hello"); }, (2+2===5), new Date() ); }
},
'log-exec3': {
'method': function () { log( "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch" ); }
}
};
// Setup the demo page for easy consumption
function setup ()
{
// Create the list of example methods and their buttons based on the object above,
// rather than writing everything twice
var ul = document.getElementById('command-list'),
i, html, buttonIds = [];
for (i in commands) {
// Create a list item with a 'Run it' button and the code that it will, er, execute
html = '<li>'
html += '<input type="button" id="' + i + '" value="Run it" title="Go ahead, drop that log!"> ';
html += '<code>';
// Omit the wrapping "function () { }" for display
html += commands[i].method.toString().replace(/^function\s\(\)\s\{\s/,'').replace(/\s\}$/,'');
html += '</code></li>';
// Add the list item to the list
ul.innerHTML += html;
buttonIds.push(i);
}
// Listen for button clicks
i = buttonIds.length;
while (i--) {
addEvent(document.getElementById(buttonIds[i]), 'click', function (event) { executeMe(event); });
}
}
// Run the method associated with a button that was clicked
function executeMe (event)
{
if (!event && window.event) { log('ie event'); event = window.event; }
var target = event.target || event.srcElement;
commands[target.id].method();
}
// (Overly?) Simple cross-browser addEventListener
function addEvent (oElem,sEventName,sFunction)
{
if (oElem.addEventListener) {
oElem.addEventListener(sEventName,sFunction,false);
}
else if (oElem.attachEvent) {
oElem.attachEvent('on' + sEventName,sFunction);
}
}
// Fill in the page
addEvent(window,'load',setup);
// Array.prototype.slice.call(arguments)