-
Notifications
You must be signed in to change notification settings - Fork 1
/
lesson18.html
318 lines (220 loc) · 9.94 KB
/
lesson18.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<!DOCTYPE html>
<html>
<head>
<title>Beginner JavaScript</title>
</head>
<body>
<script src="allPages.js"></script>
<!-- HTML Mixed CodeMirror-->
<script src="codemirror-5.42.0/lib/codemirror.js"></script>
<link rel="stylesheet" href="codemirror-5.42.0/lib/codemirror.css">
<script src="codemirror-5.42.0/mode/htmlmixed/htmlmixed.js"></script>
<script src="codemirror-5.42.0/mode/xml/xml.js"></script>
<script src="codemirror-5.42.0/mode/javascript/javascript.js"></script>
<script src="codemirror-5.42.0/mode/css/css.js"></script>
<style>
h1 {
text-align:center;
}
html, body {
margin:0;
padding:0;
}
p {
margin:8px;
}
</style>
<p>Often, you will not want to create an element using Javascript, and merely add functionality to an element.</p>
<p>This program uses document.getElementById to access an HTML element.</p>
<p>Try changing the buttons id to button2. Change any CSS and Javascript needed to make the program work as before.</p>
<script>
window.answershown = false
function showanswer() {
if (window.answershown === true) {
document.querySelector("#example > .CodeMirror").remove()
window.answershown = false
document.getElementById("solutionbutton").innerHTML = "Show Solution"
}
else {
window.answershown = true
document.getElementById("solutionbutton").innerHTML = "Hide Solution"
document.getElementById("exampleeditor").value = window.solutionHTML
var example = CodeMirror.fromTextArea(document.getElementById("exampleeditor"), {
name: "htmlmixed",
autoCloseTags: true,
styleActiveLine: true,
lineWrapping: false,
lineNumbers:true,
tabSize:2,
readOnly:true,
cursorBlinkRate:-1 //Hides cursor
});
}
}
window.solutionHTML = "<!--<!DOCTYPE html>\n<html>\n <head>\n </head>\n <body>\n <button id=\"button2\">Click me to change color</button>\n <style>\n #button2 {\n \tbackground-color:lightblue;\n border: 3px solid black;\n }\n </style>\n <script>\n\t\t\tvar button = document.getElementById(\"button2\") //Get the button with an id of button2\n button.onclick = function() {\n button.style.backgroundColor = \"lightgreen\" //Overrule the CSS styling\n \talert(\"Changed color\")\n }\n </script>\n </body>\n</html>-->";
window.solutionHTML = window.solutionHTML.slice(4)
window.solutionHTML = window.solutionHTML.slice(0, -3)
</script>
<div style="text-align:center;">
<button style="display:inline-block;margin:auto;padding:6px;background-color:aqua"onclick="showanswer()" id="solutionbutton">Show Solution</button>
<button style="display:inline-block;margin:auto;padding:6px;background-color:lightsalmon;"onclick="window.location = self.previousLesson">Go Back</button>
<button style="display:inline-block;margin:auto;padding:6px;background-color:lightgreen;"onclick="window.location = self.nextLesson">Continue</button>
</div>
<div id="example">
<textarea id="exampleeditor" hidden="true"></textarea>
</div>
<script>
//HTML that is in editor at start
//.split("\n").join("\\n").split("\t").join("\\t").split("\"").join("\\\"")
window.initialHTML = "<!--<!DOCTYPE html>\n<html>\n <head>\n </head>\n <body>\n <button id=\"button1\">Click me to change color</button>\n <style>\n #button1 {\n \tbackground-color:lightblue;\n border: 3px solid black;\n }\n </style>\n <script>\n\t\t\tvar button = document.getElementById(\"button1\") //Get the button with an id of button1\n button.onclick = function() {\n button.style.backgroundColor = \"lightgreen\" //Overrule the CSS styling\n \talert(\"Changed color\")\n }\n </script>\n </body>\n</html>-->";
window.initialHTML = window.initialHTML.slice(4)
window.initialHTML = window.initialHTML.slice(0, -3)
</script>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<!-- All the stuff below is CodeMirror-->
<!-- window.initialHTML is the HTML that they should begin with-->
<div id="editorwrapper">
<textarea id="editor"></textarea>
<button id="run">Run!</button>
<button id="download">Download!</button>
<button id="reset">Reset</button>
</div>
<iframe id="result" src="javascript:document.write('Click run - the HTML page will display here')"></iframe>
<script>
function download(content, name, mimetype) {
if (!window.Blob) {
//IE <10
var ifd = document.getElementById('dummy').contentDocument;
ifd.open('text/plain', 'replace');
ifd.write(content);
ifd.close();
ifd.execCommand('SaveAs', true, name);
}
else {
content = new Blob([content], {type:mimetype})
//IE >=10
if (navigator.msSaveBlob) {
navigator.msSaveBlob(content, name)
}
else {
//Non-IE
var link = document.createElement("a");
document.body.appendChild(link);
link.download = name
link.href = URL.createObjectURL(content);
link.click();
document.body.removeChild(link);
}
}
}
document.getElementById("editor").value = localStorage.getItem(window.location.href + "htmlcode") || window.initialHTML
var previoustext;
function save() {
var currentValue = editor.getValue()
if (editor.getValue() !== previoustext) {
previoustext = currentValue
localStorage.setItem(window.location.href + "htmlcode", currentValue)
}
if (currentValue === window.initialHTML) {
document.getElementById("reset").style.opacity = "0.4"
document.getElementById("reset").style.cursor = "not-allowed"
}
else {
document.getElementById("reset").style.opacity = "1"
document.getElementById("reset").style.cursor = "default"
}
}
setInterval(save, 100)
function resetCode() {
editor.setValue(window.initialHTML)
}
document.getElementById("reset").addEventListener("click", resetCode)
var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
name: "htmlmixed",
autoCloseTags: true,
styleActiveLine: true,
lineWrapping: false,
lineNumbers:true,
tabSize:2
});
function runcode() {
var htmlcode = editor.getValue()
var iframe = document.getElementById("result")
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(htmlcode);
iframe.contentWindow.document.close();
}
document.getElementById("run").addEventListener("click", runcode)
document.getElementById("download").addEventListener("click", function(){
download(editor.getValue(), "website.html", "text/html")
})
</script>
<style>
#editorwrapper {
position:fixed;
bottom:0;
left:0;
height:45%;
width:60%;
display:inline-block;
float:left;
padding:0;
border-right:1px solid black;
border-top:1px solid black;
}
#result {
position:fixed;
bottom:0;
right:0;
height:45%;
width:40%;
border:0;
border-top:1px solid black;
border-left:1px solid black;
display:inline-block;
float:right;
padding:0;
}
/*Make the bottom editor display over the solution*/
#editorwrapper {
background-color:white;
opacity: 1;
z-index: 4;
}
.CodeMirror {
width:100%;
height:93%;
}
#run {
width:45%;
height:7%;
font-size:16px;
background-color:#99e0ff;
border:0;
display:inline-block;
float:left;
}
#download {
width:30%;
height:7%;
font-size:16px;
background-color:#99ffe0;
border:0;
display:inline-block;
}
#reset {
width:25%;
height:7%;
font-size:16px;
background-color:salmon;
border:0;
display:inline-block;
float:right;
}
html, body {
width:100%;
height:100%;
}
</style>
</body>
</html>