Repository navigation
Expand file tree
/
Copy pathjavascript-in-html.html
More file actions
129 lines (113 loc) · 5.4 KB
/
Copy pathjavascript-in-html.html
File metadata and controls
129 lines (113 loc) · 5.4 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- Force IE to use its standard document rendering mode - I found this line at https://stackoverflow.com/questions/10975107/forcing-internet-explorer-9-to-use-standards-document-mode -->
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta name="viewport" content="width=device-width">
<title>JavaScript in HTML | Jschool</title>
<link type="text/css" rel="stylesheet" href="css/style.css">
<link rel="shortcut icon" href="images/js-icon.png" type="image/x-icon">
</head>
<body>
<header>
<!-- Page header containing logo/site title, with a navigation bar
nested inside of it-->
<a href="index.html"><h1 id="header-logo">Jschool</h1></a>
<!-- navigation bar in the semantically nice nav element -->
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="index.html#tutorial-section">Tutorials</a></li>
<li><a href="quizzes.html">Quizzes</a></li>
<li><a href="about.html">About</a></li>
</ul>
</nav>
</header>
<div id="tutorial-container" class="fade-in">
<section class="lead">
<h1>JavaScript in HTML</h1>
<p>It's extremely easy to mix JavaScript into HTML, in a variety of ways, to allow your new found JavaScript
skills to bring your web pages to life - as this tutorial demonstrates.</p>
</section>
<section>
<h2>Inline with Script Tags</h2>
<p>The simplest way to add JavaScript into HTML is to use the <script> tags - as shown below...</p>
<!-- Code snipping to follow... no indentation inside of code tags because the pre tag forces them to be un-formatted, so indentation does not work! -->
<pre>
<code>
<html>
<head>
<title>JavaScript Test</title>
</head>
<body>
<script>
/* This JavaScript is ran whilst the page is being rendered... */
console.log("Hello world!");
document.querySelector("body").textContent = "Hello (again)";
</script>
</body>
</html>
</code>
</pre>
<p>This makes the script within the tags get run as the page is rendered.</p>
<p class="top-tip">This approach is not recommended for anything larger than simple testing - for real world
web pages/websites, the approach described next should be adopted instead...</p>
</section>
<section>
<h2>In external files</h2>
<p>The best way to link JavaScript into HTML is to use the <script> tags to link to external files. The
file path is relative to your HTML document (just like the linking of external CSS files). An example of
this in action is shown below...</p>
<!-- Code snipping to follow... no indentation inside of code tags because the pre tag forces them to be un-formatted, so indentation does not work! -->
<pre>
<code>
<html>
<head>
<title>JavaScript Test</title>
</head>
<body>
<script src="path/to/the/script.js"></script>
</body>
</html>
</code>
</pre>
<p class="top-tip"><script> links should be located at the end of the document. This is so that the HTML
can be rendered properly first, before JavaScript is executed.</p>
</section>
<section>
<h2>Does order matter?</h2>
<p>Yes! Scripts which refer to variables or functions defined in other scripts must be linked before scripts
that rely on them.</p>
<h2>What about scope?</h2>
<p>By default, everything in JavaScript has a global scope once included - for this reason, variables and
functions are usually put within objects, to stop names from colliding easily, and to provide a basic
namespace system.</p>
<h2>Can JavaScript files import other JavaScript files within them (like CSS' @import)?</h2>
<p>No - not natively. However, a library called <a href="http://requirejs.org/">RequireJS</a> makes this
possible (and is coincidentally used within this website).</p>
<p class="top-tip">In HTML versions lower than HTML5, the <script> tag requires the type="text/javascript"
attribute to be included. In HTML5, this attribute's information is assumed automatically, and it is not
required.</p>
</section>
</div>
<!-- Consistent footer containing links and other information -->
<footer>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="index.html#tutorial-section">Tutorials</a></li>
<li><a href="quizzes.html">Quizzes</a></li>
<li><a href="about.html">About</a></li>
<li><a href="credits.html">Credits</a></li>
</ul>
<p>© Dylan McKee 2015 - for Newcastle University.</p>
</footer>
<!-- linking JavaScript at the end for page load speed benefits.
Using RequireJS has meant that I only need to link one file into the actual
HTML, allowing my HTML to be semantically beautiful.
I can also load JavaScript in Async, which is ideal to speed up loading,
again thanks to RequireJS.
-->
<script data-main="js/main" type="text/javascript" src="js/lib/require.js" async></script>
</body>
</html>