-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbad-javascript.html
More file actions
159 lines (142 loc) · 4.38 KB
/
bad-javascript.html
File metadata and controls
159 lines (142 loc) · 4.38 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Bad Javascript!</title>
</head>
<body>
<h1>Bad Javascript!</h1>
<h3>CoffeeScript source</h3>
<pre>
# jQuery references we'll use more than once
$body = $('body')
# Set the state of the interface by adding themer-state-XX as a class on body, and clearing others.
set_state = (state) ->
window.alert('Requested state: '+state)
# States:
#
# 00. Prompting for input
# 01. (Reserved for re-theming)
# 10. Beginning theme process
# 20. Theme actively loading
# 30. Initial preview loaded
# 40. Element selected
# 50. Content replaced with dummy content
# 60. Saving
states = '00 01 10 20 30 40 50 60'.split(' ')
as_class = (str) -> 'themer-state-'+str
state_classes = (as_class(state) for state in states)
$body.removeClass(state_classes.join ' ') # can pass as one space-separated list
$body.addClass as_class(state)
window.alert('Set state: '+state+' (should be the same as before)')
# Initial page load, so
set_state '00'</pre>
<h3>Output JavaScript</h3>
<pre>var $body, set_state;
$body = $('body');
set_state = function(state) {
var as_class, state, state_classes, states;
window.alert('Requested state: ' + state);
states = '00 01 10 20 30 40 50 60'.split(' ');
as_class = function(str) {
return 'themer-state-' + str;
};
state_classes = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = states.length; _i < _len; _i++) {
state = states[_i];
_results.push(as_class(state));
}
return _results;
})();
$body.removeClass(state_classes.join(' '));
$body.addClass(as_class(state));
return window.alert('Set state: ' + state + ' (should be the same as before)');
};
set_state('00');</pre>
<h3>Run…</h3>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">function test_it(){
var $body, set_state;
$body = $('body');
set_state = function(state) {
var as_class, state, state_classes, states;
window.alert('Requested state: ' + state);
states = '00 01 10 20 30 40 50 60'.split(' ');
as_class = function(str) {
return 'themer-state-' + str;
};
state_classes = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = states.length; _i < _len; _i++) {
state = states[_i];
_results.push(as_class(state));
}
return _results;
})();
$body.removeClass(state_classes.join(' '));
$body.addClass(as_class(state));
return window.alert('Set state: ' + state + ' (should be the same as before)');
};
set_state('00');
}</script>
<button name="test-it" value="test-it" onclick="test_it();">
Test It
</button>
<h3>JavaScript</h3>
<pre>var $body, set_state;
$body = $('body');
set_state = function(state) {
var as_class, state, state_classes, states;
window.alert('Requested state: ' + state);
states = '00 01 10 20 30 40 50 60'.split(' ');
as_class = function(str) {
return 'themer-state-' + str;
};
state_classes = (function() {
var _i, _len, _results, state;
_results = [];
for (_i = 0, _len = states.length; _i < _len; _i++) {
state = states[_i];
_results.push(as_class(state));
}
return _results;
})();
$body.removeClass(state_classes.join(' '));
$body.addClass(as_class(state));
return window.alert('Set state: ' + state + ' (should be the same as before)');
};
set_state('00');</pre>
<h3>Run…</h3>
<script type="text/javascript">function test_fixed(){
var $body, set_state;
$body = $('body');
set_state = function(state) {
var as_class, state, state_classes, states;
window.alert('Requested state: ' + state);
states = '00 01 10 20 30 40 50 60'.split(' ');
as_class = function(str) {
return 'themer-state-' + str;
};
state_classes = (function() {
var _i, _len, _results, state;
_results = [];
for (_i = 0, _len = states.length; _i < _len; _i++) {
state = states[_i];
_results.push(as_class(state));
}
return _results;
})();
$body.removeClass(state_classes.join(' '));
$body.addClass(as_class(state));
return window.alert('Set state: ' + state + ' (should be the same as before)');
};
set_state('00');
}</script>
<button name="test-fixed" value="test-fixed" onclick="test_fixed();">
Test Fixed Version
</button>
</body>
</html>