-
Notifications
You must be signed in to change notification settings - Fork 1
/
lesson20.html
288 lines (204 loc) · 9.47 KB
/
lesson20.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
<!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>You can also use CSS selectors to select elements through document.querySelector and document.querySelectorAll</p>
<p>Let's use document.querySelector to find and modify some HTML elements!</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 \n <p id=\"paragraph1\">This is the first paragraph</p>\n <p class=\"paragraph\">This is the second paragraph</p>\n <p>This is the third paragraph</p>\n <p>This is the fourth paragraph</p>\n\n <style>\n #paragraph1 {\n \tfont-weight:bold;\n }\n </style>\n <script>\n\t\t\t//That first paragraph isn't fancy enough... Let's make it italic!\n\t\t\tvar paragraph1 = document.querySelector(\"#paragraph1\")\n paragraph1.style.fontStyle = \"italic\"\n \n //Let's give that second paragraph a little more pop! Try making it blue!\n var paragraph2 = document.querySelector(\".paragraph\")\n paragraph2.style.color = \"blue\"\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>
<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>
<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 \n <p id=\"paragraph1\">This is the first paragraph</p>\n <p class=\"paragraph\">This is the second paragraph</p>\n <p>This is the third paragraph</p>\n <p>This is the fourth paragraph</p>\n\n <style>\n #paragraph1 {\n \tfont-weight:bold;\n }\n </style>\n <script>\n\t\t\t//That first paragraph isn't fancy enough... Let's make it italic!\n\t\t\tvar paragraph1 = document.querySelector(\"#paragraph1\")\n paragraph1.style.fontStyle = \"italic\"\n \n //Let's give that second paragraph a little more pop! Try making it blue!\n //Hint: With classes, the selector is always .className\n var paragraph2 = document.querySelector(\"\") //Fill in the CSS selector\n paragraph2.style.color = \"blue\"\n \n </script>\n </body>\n</html>-->";
window.initialHTML = window.initialHTML.slice(4)
window.initialHTML = window.initialHTML.slice(0, -3)
</script>
<!-- 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) {
var link = document.createElement("a");
document.body.appendChild(link);
content = new Blob([content], {type:mimetype})
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 {
left:0;
height:50%;
width:60%;
display:inline-block;
float:left;
border-right:1px solid black;
border-top:1px solid black;
position:fixed;
bottom:0;
padding:0;
}
#result {
right:0;
height:50%;
width:40%;
border:0;
border-top:1px solid black;
border-left:1px solid black;
display:inline-block;
float:right;
position:fixed;
bottom:0;
padding:0;
background-color:white;
z-index:4;
}
/*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>