-
Notifications
You must be signed in to change notification settings - Fork 1
/
lesson2.html
310 lines (209 loc) · 8.78 KB
/
lesson2.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
<!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>
<pre id="exampletext" hidden="true">
//Get a value from the user
value = prompt("Please type in a number")
alert("You picked the number " + value)</pre>
<p>Javascript may seem like "just another language" - but there is something special about JavaScript - it can run in the browser.</p>
<p>JavaScript can be inserted in HTML files using script tags - much like paragraph tags. An example is shown in the editor in the left hand window below.
When you click "Run", the program will execute right on this page!</p>
<p>You can also Click the "Download" button to download the HTML file, then open it in your browser (usually you can just click on it, though a right click and "Open With" may be needed)</p>
<p>Try this now by clicking the Download button and then open the HTML in your browser.</p>
<p>Congratulations on Your First Website! We'll be adding more to it in the next lessons. </p>
<div id="example">
<textarea id="exampleeditor"></textarea>
</div>
<div style="text-align:center;">
<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>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<script>
document.getElementById("exampleeditor").value = document.getElementById("exampletext").innerText
var example = CodeMirror.fromTextArea(document.getElementById("exampleeditor"), {
mode: "javascript",
autoCloseTags: true,
styleActiveLine: true,
lineWrapping: false,
lineNumbers:true,
readOnly:true,
tabSize:2,
cursorBlinkRate:-1 //Hides cursor
});
</script>
<script>
//HTML that is in editor at start
//.split("\n").join("\\n").split("\t").join("\\t").split("\"").join("\\\"")
//Then add HTML comments to start and end of string
window.initialHTML = "<!--<!DOCTYPE html>\n<html>\n <head>\n </head>\n <body>\n <p>This is a HTML website with embedded JavaScript!</p>\n <style>\n body {\n \tbackground-color: antiquewhite;\n }\n </style>\n <script>\n\n\n //Get a value from the user\n value = prompt(\"Please type in a number\")\n alert(\"You picked the number \" + value)\n\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>
<!-- iframe purely for IE <10 downloads -->
<iframe id="dummy" style="display:none; visibility:hidden"></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)
setInterval(save, 1000)
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;
}
.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>