2 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en" lang=
"en">
4 <title>Testing Javascript
</title>
5 <script type=
"text/javascript">
8 function updateContent(input) {
9 document.getElementById('result').innerHTML =
"<p>" + input.value +
"</p>";
12 function displayMessage(message) {
13 document.getElementById('result').innerHTML =
"<p>" + message +
"</p>";
16 function appendMessage(message) {
17 document.getElementById('result').innerHTML += message +
" ";
20 function register(message) {
22 appendMessage(message);
27 function delayedShowHide(delay, show) {
28 var blackBox = document.getElementById('clickToHide');
29 window.setTimeout(function() {
30 blackBox.style.display = show ? '' : 'none';
34 <script type=
"text/javascript">
35 var startList = function() {
36 // Ugh. Let's hope no-one is faking their user agent when running the tests
37 if (navigator.userAgent.indexOf(
"MSIE") != -
1) {
38 var navRoot = document.getElementById(
"nav");
39 for (var i =
0; i < navRoot.childNodes.length; i++) {
40 var node = navRoot.childNodes[i];
41 if (node.nodeName ==
"LI") {
42 node.onmouseover = function() {
43 this.className +=
" over";
45 node.onmouseout = function() {
46 this.className = this.className.replace(
" over",
"");
52 window.onload=startList;
54 <style type=
"text/css">
56 padding:
0; margin:
0; list-style: none;
59 float: left; position: relative; width:
10em;
62 display: none; position: absolute; top:
1em; left:
0;
64 #nav li
> ul { top: auto; left: auto; }
65 #nav li:hover ul, #nav li.over ul{ display: block; background: white; }
75 <li id=
"item1" onclick=
"displayMessage('item 1');">Item
1</li>
82 <div id=
"resultContainer" height=
"30">
83 <div id=
"result" style=
"width:300;height:60">
89 <div id=
"formageddon">
91 Key Up:
<input type=
"text" id=
"keyUp" onkeyup=
"javascript:updateContent(this)"/><br/>
92 Key Down:
<input type=
"text" id=
"keyDown" onkeydown=
"javascript:updateContent(this)"/><br/>
93 Key Press:
<input type=
"text" id=
"keyPress" onkeypress=
"javascript:updateContent(this)"/><br/>
94 Change:
<input type=
"text" id=
"change" onkeypress=
"javascript:displayMessage('change')"/><br/>
95 <textarea id=
"keyDownArea" onkeydown=
"javascript:updateContent(this)" rows=
"2" cols=
"15"></textarea>
96 <textarea id=
"keyPressArea" onkeypress=
"javascript:updateContent(this)" rows=
"2" cols=
"15"></textarea>
97 <textarea id=
"keyUpArea" onkeyup=
"javascript:updateContent(this)" rows=
"2" cols=
"15"></textarea>
98 <select id=
"selector" onchange=
"javascript:updateContent(this)">
99 <option value=
"foo">Foo
</option>
100 <option value=
"bar">Bar
</option>
102 <input type=
"checkbox" id=
"checkbox" value=
"checkbox thing" onchange=
"javascript:updateContent(this)"/>
103 <input id=
"clickField" type=
"text" onclick=
"document.getElementById('clickField').value='Clicked';" value=
"Hello"/>
104 <input id=
"doubleClickField" type=
"text" onclick=
"document.getElementById('doubleClickField').value='Clicked';" ondblclick=
"document.getElementById('doubleClickField').value='DoubleClicked';" oncontextmenu=
"document.getElementById('doubleClickField').value='ContextClicked'; return false;" value=
"DoubleHello"/>
105 <input id=
"clearMe" value=
"Something" onchange=
"displayMessage('Cleared')"/>
110 <p><a href=
"#" onclick=
"javascript:document.title='Changed'">Change the page title!
</a></p>
112 <p><a onclick=
"javascript:document.title='Changed'" id=
"nohref">No href
</a></p>
114 <p><a id=
"updatediv" href=
"#" onclick=
"javascript:document.getElementById('dynamo').innerHTML = 'Fish and chips!';">Update a
118 <div id=
"dynamo">What's for dinner?
</div>
120 <div id=
"mousedown" onmousedown=
"javascript:displayMessage('mouse down');">
121 <p>Click for the mouse down event
</p>
122 <span><p id=
"child">Here's some text
</p></span>
125 <div id=
"mouseup" onmouseup=
"javascript:displayMessage('mouse up');">
126 <p>Click for the mouse up event
</p>
129 <div id=
"mouseclick" onclick=
"javascript:displayMessage('mouse click');">
130 <p>Click for the mouse click event
</p>
133 <div id=
"error" onclick=
"document.getElementById('doesnotexist').innerHTML = 'cheese';">
134 Clicking this causes a JS exception in the click handler
138 <form action=
"resultPage.html" id=
"on-form">
140 onfocus=
"appendMessage('focus')"
141 onkeydown=
"appendMessage('keydown')"
142 onkeypress=
"appendMessage('keypress')"
143 onkeyup=
"appendMessage('keyup')"
144 onblur=
"appendMessage('blur')"
145 onchange=
"appendMessage('change')"
148 <input id=
"changeable" name=
"changeable" onfocus=
"appendMessage('focus')" onchange=
"appendMessage('change')" onblur=
"appendMessage('blur')"/>
150 <button type=
"button" id=
"plainButton"
151 onfocus=
"appendMessage('focus')"
152 onkeydown=
"appendMessage('keydown')"
153 onkeypress=
"appendMessage('keypress')"
154 onkeyup=
"appendMessage('keyup')"
155 onblur=
"appendMessage('blur')"
156 onclick=
"appendMessage('click')"
157 onmousedown=
"appendMessage('mousedown ')"
158 onmouseup=
"appendMessage('mouseup ')"
159 onmouseover=
"register('mouseover ')"
160 onmousemove=
"register('mousemove ')"
164 <button type=
"submit" id=
"submittingButton"><emph>submit
</emph></button>
165 <button type=
"button" id=
"jsSubmitButton" onclick=
"javascript:document.getElementById('on-form').submit();">Submitomatic
</button>
167 <button type=
"button" id=
"switchFocus" onclick=
"document.getElementById('theworks').focus();">Switch focus
</button>
168 <button type=
"button" onclick=
"var element = document.getElementById('switchFocus'); var clickEvent = document.createEvent('MouseEvents'); clickEvent.initMouseEvent('click', true, true, null, 0, 0, 0, 0, 0,false, false, false, false, 0, element);element.dispatchEvent(clickEvent);">Do magic
</button><br/>
169 <label id=
"labelForCheckbox" for=
"labeledCheckbox" onclick=
"appendMessage('labelclick')">Toggle checkbox
</label><input type=
"checkbox" id=
"labeledCheckbox" onclick=
"appendMessage('chboxclick')"/>
172 <form action=
"javascriptPage.html" id=
"submitListeningForm" onsubmit=
"appendMessage('form-onsubmit '); return false;">
174 <input id=
"submitListeningForm-text" type=
"text" onsubmit=
"appendMessage('text-onsubmit ')" onclick=
"appendMessage('text-onclick ');" />
175 <input id=
"submitListeningForm-submit" type=
"submit" onsubmit=
"appendMessage('submit-onsubmit ')" onclick=
"appendMessage('submit-onclick ');" />
180 <p id=
"suppressedParagraph" style=
"display: none">A paragraph suppressed using CSS display=none
</p>
183 <p id=
"displayed">Displayed
</p>
185 <form action=
"#"><input type=
"hidden" name=
"hidden" /> </form>
187 <p id=
"none" style=
"display: none;">Display set to none
</p>
189 <p id=
"hidden" style=
"visibility: hidden;">Hidden
</p>
191 <div id=
"hiddenparent" style=
"height: 2em; display: none;">
192 <div id=
"hiddenchild">
193 <a href=
"#" id=
"hiddenlink">ok
</a>
197 <div style=
"visibility: hidden;">
199 <input id=
"unclickable" />
200 <input type=
"checkbox" id=
"untogglable" checked=
"checked" />Check box you can't see
204 <p id=
"outer" style=
"visibility: hidden">A
<b id=
"visibleSubElement" style=
"visibility: visible">sub-element that is explicitly visible
</b> using CSS visibility=visible
</p>
209 <input type=
"text" id=
"keyReporter" size=
"80"
210 onkeyup=
"appendMessage('up: ' + event.keyCode)"
211 onkeypress=
"appendMessage('press: ' + event.keyCode)"
212 onkeydown=
"displayMessage(''); appendMessage('down: ' + event.keyCode)" />
213 <input name=
"suppress" onkeydown=
"if (event.preventDefault) event.preventDefault(); event.returnValue = false; return false;" onkeypress=
"appendMessage('press');"/>
217 <!-- Used for testing styles -->
218 <div style=
"background-color: green;" id=
"green-parent">
219 <p id=
"style1">This should be greenish
</p>
221 <li id=
"green-item">So should this
</li>
222 <li id=
"red-item" style=
"background-color: red;">But this is red
</li>
226 <a href=
"#" id=
"close" onclick=
"window.close();">Close window
</a>
228 <div id=
"delete" onclick=
"var d = document.getElementById('deleted'); this.removeChild(d);">
229 <p id=
"deleted">I should be deleted when you click my containing div
</p>
230 <p>Whereas, I should not
</p>
234 <span id=
"hideMe" onclick=
"this.style.display = 'none';">Click to hide me.
</span>
237 <div style=
"margin-top: 10px;">
238 Click actions delayed by
3000ms:
239 <div id=
"clickToShow" onclick=
"delayedShowHide(3000, true);"
240 style=
"float: left;width: 100px;height:100px;border: 1px solid black;">
241 Click to show black box
243 <div id=
"clickToHide" onclick=
"delayedShowHide(3000, false);"
244 style=
"float: left;width: 100px;height:100px;border: 1px solid black;
245 background-color: black; color: white; display: none;">
246 Click to hide black box
248 <div style=
"clear: both"></div>
251 <a id=
"new_window" onmouseup=
"window.open('closeable_window.html', 'close_me')" href=
"#">Click me to open a new window
</a>
253 <a id=
"throwing-mouseover" onmouseover=
"throw new Error()" href=
"#throwing-mouseover">Mouse over me will throw a JS error
</a>
256 <span id=
"movable" onmouseover=
"var p = document.getElementById('movable'); displayMessage('parent matches? ' + (p != event.relatedTarget));">
257 Click on me to show the related target
261 <div id=
"zero" style=
"width:0;height:0">