|
4 | 4 | <title>Beginner JavaScript</title> |
5 | 5 | </head> |
6 | 6 | <body> |
| 7 | + |
7 | 8 | <script src="allPages.js"></script> |
8 | 9 |
|
| 10 | + |
| 11 | + |
9 | 12 | <!-- HTML Mixed CodeMirror--> |
10 | 13 | <script src="codemirror-5.42.0/lib/codemirror.js"></script> |
11 | 14 | <link rel="stylesheet" href="codemirror-5.42.0/lib/codemirror.css"> |
|
15 | 18 | <script src="codemirror-5.42.0/mode/css/css.js"></script> |
16 | 19 |
|
17 | 20 |
|
18 | | - |
19 | | - |
20 | 21 | <style> |
21 | 22 | h1 { |
22 | 23 | text-align:center; |
|
35 | 36 |
|
36 | 37 |
|
37 | 38 |
|
38 | | - <p>Now try writing a function. Your function will take two numbers, add them together, and alert the user of the answer.</p> |
39 | | - |
40 | | - <p>When called with the numbers 14 and 28, your function should alert 42</p> |
41 | | - |
42 | | - |
43 | | - <script> |
44 | | - window.answershown = false |
45 | | - |
46 | | - function showanswer() { |
47 | | - |
48 | | - if (window.answershown === true) { |
49 | | - document.querySelector("#example > .CodeMirror").remove() |
50 | | - window.answershown = false |
51 | | - document.getElementById("solutionbutton").innerHTML = "Show Solution" |
52 | | - } |
53 | | - else { |
54 | | - window.answershown = true |
55 | | - document.getElementById("solutionbutton").innerHTML = "Hide Solution" |
56 | | - |
57 | | - |
58 | | - |
59 | | - document.getElementById("exampleeditor").value = window.solutionHTML |
60 | | - |
61 | | - var example = CodeMirror.fromTextArea(document.getElementById("exampleeditor"), { |
62 | | - name: "htmlmixed", |
63 | | - autoCloseTags: true, |
64 | | - styleActiveLine: true, |
65 | | - lineWrapping: false, |
66 | | - lineNumbers:true, |
67 | | - tabSize:2, |
68 | | - readOnly:true, |
69 | | - cursorBlinkRate:-1 //Hides cursor |
70 | | - }); |
71 | | - } |
72 | | -} |
| 39 | + <p>Paragraph elements aren't that entertaining - lets create a button!</p> |
73 | 40 |
|
74 | | - window.solutionHTML = "<!--<!DOCTYPE html>\n<html>\n <head>\n </head>\n <body>\n <script>\n\t\t\tfunction addAndAlert(number1, number2) {\n \tvar sum = number1 + number2\n alert(\"The sum of \" + number1 + \" and \" + number2 + \" is \" + sum)\n }\n addAndAlert(14,28)\n </script>\n </body>\n</html>-->"; |
75 | 41 |
|
76 | | - window.solutionHTML = window.solutionHTML.slice(4) |
77 | | - window.solutionHTML = window.solutionHTML.slice(0, -3) |
78 | | - </script> |
79 | 42 |
|
80 | 43 |
|
81 | 44 | <div style="text-align:center;"> |
82 | | - <button style="display:inline-block;margin:auto;padding:6px;background-color:aqua"onclick="showanswer()" id="solutionbutton">Show Solution</button> |
83 | | - |
84 | 45 | <button style="display:inline-block;margin:auto;padding:6px;background-color:lightsalmon;"onclick="window.location = self.previousLesson">Go Back</button> |
85 | 46 |
|
86 | 47 | <button style="display:inline-block;margin:auto;padding:6px;background-color:lightgreen;"onclick="window.location = self.nextLesson">Continue</button> |
87 | | -</div> |
88 | 48 |
|
89 | | - <div id="example"> |
| 49 | + </div> |
| 50 | + |
| 51 | + <div id="example"> |
90 | 52 | <textarea id="exampleeditor" hidden="true"></textarea> |
91 | 53 | </div> |
92 | 54 |
|
93 | | - <br><br><br><br><br><br><br><br> |
94 | 55 |
|
95 | 56 |
|
96 | 57 |
|
97 | | - <script> |
98 | | - //HTML that is in editor at start |
99 | | - //.split("\n").join("\\n").split("\t").join("\\t").split("\"").join("\\\"") |
100 | | - window.initialHTML = "<!--<!DOCTYPE html>\n<html>\n <head>\n </head>\n <body>\n <script>\n\t\t\tfunction addAndAlert(number1, number2) {\n \t//Your code here\n }\n addAndAlert(14,28)\n </script>\n </body>\n</html>-->"; |
101 | | - |
102 | | - window.initialHTML = window.initialHTML.slice(4) |
103 | | - window.initialHTML = window.initialHTML.slice(0, -3) |
104 | | - |
105 | | - |
106 | | - </script> |
107 | 58 |
|
108 | 59 |
|
109 | 60 |
|
110 | 61 |
|
| 62 | + <script> |
111 | 63 |
|
112 | 64 |
|
| 65 | + //HTML that is in editor at start |
| 66 | + //.split("\n").join("\\n").split("\t").join("\\t").split("\"").join("\\\"") |
| 67 | + window.initialHTML = "<!--<!DOCTYPE html>\n<html>\n <head>\n </head>\n <body>\n <style>\n html, body {\n \tbackground-color:antiquewhite;\n }\n </style>\n <script>\n \n paragraph = document.createElement(\"p\") //Create a paragraph element\n paragraph.innerHTML = \"This is the text (HTML) inside the paragraph element\"\n document.body.appendChild(paragraph) //Add the paragraph to the page\n \n \n </script>\n </body>\n</html>-->"; |
113 | 68 |
|
| 69 | + window.initialHTML = window.initialHTML.slice(4) |
| 70 | + window.initialHTML = window.initialHTML.slice(0, -3) |
114 | 71 |
|
115 | 72 |
|
116 | 73 |
|
| 74 | + </script> |
117 | 75 |
|
118 | 76 |
|
119 | 77 |
|
|
126 | 84 | <!-- window.initialHTML is the HTML that they should begin with--> |
127 | 85 |
|
128 | 86 |
|
| 87 | + |
129 | 88 | <div id="editorwrapper"> |
130 | 89 | <textarea id="editor"></textarea> |
131 | 90 | <button id="run">Run!</button> |
|
135 | 94 |
|
136 | 95 | <iframe id="result" src="javascript:document.write('Click run - the HTML page will display here')"></iframe> |
137 | 96 |
|
138 | | - |
139 | 97 | <script> |
140 | 98 |
|
141 | 99 | function download(content, name, mimetype) { |
| 100 | + |
| 101 | + if (!window.Blob) { |
| 102 | + //IE <10 |
| 103 | + var ifd = document.getElementById('dummy').contentDocument; |
| 104 | + ifd.open('text/plain', 'replace'); |
| 105 | + ifd.write(content); |
| 106 | + ifd.close(); |
| 107 | + ifd.execCommand('SaveAs', true, name); |
| 108 | + } |
| 109 | + else { |
| 110 | + content = new Blob([content], {type:mimetype}) |
| 111 | + |
| 112 | + //IE >=10 |
| 113 | + if (navigator.msSaveBlob) { |
| 114 | + navigator.msSaveBlob(content, name) |
| 115 | + } |
| 116 | + else { |
| 117 | + //Non-IE |
142 | 118 | var link = document.createElement("a"); |
143 | 119 | document.body.appendChild(link); |
144 | | - content = new Blob([content], {type:mimetype}) |
| 120 | + |
145 | 121 | link.download = name |
146 | 122 | link.href = URL.createObjectURL(content); |
147 | 123 | link.click(); |
| 124 | + |
148 | 125 | document.body.removeChild(link); |
| 126 | + |
| 127 | + } |
| 128 | + } |
149 | 129 | } |
150 | 130 |
|
151 | 131 | document.getElementById("editor").value = localStorage.getItem(window.location.href + "htmlcode") || window.initialHTML |
|
185 | 165 | tabSize:2 |
186 | 166 | }); |
187 | 167 |
|
| 168 | + |
188 | 169 | function runcode() { |
189 | 170 | var htmlcode = editor.getValue() |
190 | 171 | var iframe = document.getElementById("result") |
|
206 | 187 | <style> |
207 | 188 |
|
208 | 189 | #editorwrapper { |
| 190 | + position:fixed; |
| 191 | + bottom:0; |
209 | 192 | left:0; |
210 | | - height:50%; |
| 193 | + height:45%; |
211 | 194 | width:60%; |
212 | 195 | display:inline-block; |
213 | 196 | float:left; |
| 197 | + padding:0; |
214 | 198 | border-right:1px solid black; |
215 | 199 | border-top:1px solid black; |
216 | | - position:fixed; |
217 | | - bottom:0; |
218 | | - padding:0; |
219 | | - } |
| 200 | + } |
220 | 201 |
|
221 | 202 | #result { |
| 203 | + position:fixed; |
| 204 | + bottom:0; |
222 | 205 | right:0; |
223 | | - height:50%; |
| 206 | + height:45%; |
224 | 207 | width:40%; |
225 | 208 | border:0; |
226 | 209 | border-top:1px solid black; |
227 | 210 | border-left:1px solid black; |
228 | 211 | display:inline-block; |
229 | 212 | float:right; |
230 | | - position:fixed; |
231 | | - bottom:0; |
232 | | - padding:0; |
233 | | - background-color:white; |
234 | | - z-index:4; |
235 | | - } |
236 | | - |
237 | | - /*Make the bottom editor display over the solution*/ |
238 | | - #editorwrapper { |
239 | | - background-color:white; |
240 | | - opacity: 1; |
241 | | - z-index: 4; |
| 213 | + padding:0; |
242 | 214 | } |
243 | 215 |
|
244 | 216 | .CodeMirror { |
|
276 | 248 | float:right; |
277 | 249 | } |
278 | 250 |
|
| 251 | + /*Make the bottom editor display over the solution*/ |
| 252 | + #editorwrapper { |
| 253 | + background-color:white; |
| 254 | + opacity: 1; |
| 255 | + z-index: 4; |
| 256 | + } |
| 257 | + |
| 258 | + |
279 | 259 | html, body { |
280 | 260 | width:100%; |
281 | 261 | height:100%; |
|
0 commit comments