forked from kentbrew/Case-Hardened-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
38 lines (37 loc) · 1.45 KB
/
index.html
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
<!doctype html>
<html>
<head>
<title>Case-Hardened JavaScript Tests</title>
</head>
<body>
<p>Making sure some of the assumptions I've made about my new framework are true.</p>
<ol>
<li>Hard-coded <code>script</code> tags always work, since they show up before <code>window.onload</code> fires.</li>
<li>Dynamically-created <code>script</code> tags work, if they are created before <code>window.onload</code> fires.</li>
<li>Dynamically-created <code>script</code> tags do NOT work if they are created AFTER <code>window.onload</code> fires.</li>
</ol>
<script>
var b = document.createElement('BUTTON');
b.innerHTML = 'Click to add a button.';
b.onclick = function() {
// dynamically create a NEW instance of stub.js
var s = document.createElement('SCRIPT');
s.src = 'stub.js';
// settings is a non-existant attribute, so Firefox will want us to use setAttribute and not just s.settings='foo'
s.setAttribute('settings', 'exe=1');
// exe=set means "run me immediately."
document.getElementsByTagName('BODY')[0].appendChild(s);
}
document.getElementsByTagName('BODY')[0].appendChild(b);
// this should NOT work, since it's firing after the window loads, without
// setting exe
window.onload = function() {
var s = document.createElement('SCRIPT');
s.src = 'stub.js';
document.getElementsByTagName('BODY')[0].appendChild(s);
};
</script>
<!-- this should always work -->
<script src="stub.js"></script>
</body>
</html>