Skip to content

Commit 2889572

Browse files
Markus Amalthea Magnusonajpiano
Markus Amalthea Magnuson
authored andcommitted
Various small fixes on the JavaScript 101 Running Code page. Fixes jquery#346.
1 parent 6b66941 commit 2889572

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

page/javascript-101/running-code.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ attribution:
1111
The first and recommended option is to write code in an external file (with a `.js` extension), which can then be included on our web page using an HTML `<script>` tag and pointing the `src` attribute to the file's location. Having JavaScript in a separate file will reduce code duplication if you want to reuse it on other pages. It will also allow the browser to cache the file on the remote client's computer, decreasing page load time.
1212

1313
```
14-
<!-- Code is written in a .js file, then included via the script tag src attribute. -->
14+
<!-- Code is written in a .js file, included via the script tag src attribute. -->
1515
<script src="/path/to/example.js"></script>
1616
```
1717
### Inline
@@ -21,7 +21,7 @@ The second option is to inline the code directly on the web page. This is also a
2121
```
2222
<!-- Embed code directly on a web page using script tags. -->
2323
<script>
24-
alert( "Hello World!" );
24+
alert( "Hello World!" );
2525
</script>
2626
```
2727

@@ -31,45 +31,47 @@ The last option is to use the event handler attributes of HTML elements. This me
3131

3232
```
3333
<!-- Inline code directly on HTML elements being clicked. -->
34-
<a href="javascript:alert('Hello World');">Click Me!</a>
35-
<button onClick="alert('Good Bye World');">Click Me Too!</a>
34+
<a href="javascript:alert( 'Hello World' );">Click Me!</a>
35+
<button onClick="alert( 'Good Bye World' );">Click Me Too!</a>
3636
```
3737

3838
### Placement
3939

40-
Placement of the previous two options is important and can vary depending on the situation. If you are including JavaScript that doesn't access the elements on the page, you can safely place the script before the closing HTML `<head>` tag. However, if the code will interact with the elements on the page, you have to make sure those elements exist at the time the script is executed. This common pitfall can be seen in the example below. The script for finding the element with the ID "hello-world" will be executed before the element is defined in the document.
40+
Placement of the previous two options is important and can vary depending on the situation. If you are including JavaScript that doesn't access the elements on the page, you can safely place the script before the closing HTML `<head>` tag. However, if the code will interact with the elements on the page, you have to make sure those elements exist at the time the script is executed. This common pitfall can be seen in the example below. The script for finding the element with the ID `hello-world` will be executed before the element is defined in the document.
4141

4242
```
43-
<!-- Attempting to access an element too early will have unexpected results. -->
4443
<!doctype html>
4544
<html>
4645
<head>
4746
<script>
47+
// Attempting to access an element too early will have unexpected results.
4848
var title = document.getElementById( "hello-world" );
4949
console.log( title );
5050
</script>
5151
</head>
5252
<body>
53-
<h1 id="hello-world">Hello World</h1>
53+
54+
<h1 id="hello-world">Hello World</h1>
55+
5456
</body>
5557
</html>
5658
```
5759

58-
It is a common pattern to move scripts to the bottom of the page, prior to the closing HTML `<body>` tag. This will guarantee that elements are defined when the script is executed.
60+
It is a common pattern to move scripts to the bottom of the page, prior to the closing HTML `<body>` tag. This will guarantee that elements are defined when the script is executed:
5961

6062
```
61-
<!-- Moving the script to the bottom of the page will -->
62-
<!-- make sure the element exists. -->
6363
<!doctype html>
6464
<html>
65-
<head>
66-
</head>
65+
<head></head>
6766
<body>
68-
<h1 id="hello-world">Hello World</h1>
69-
<script>
70-
var title = document.getElementById( "hello-world" );
71-
console.log( title );
72-
</script>
67+
68+
<h1 id="hello-world">Hello World</h1>
69+
<script>
70+
// Moving the script to the bottom of the page will make sure the element exists.
71+
var title = document.getElementById( "hello-world" );
72+
console.log( title );
73+
</script>
74+
7375
</body>
7476
</html>
7577
```

0 commit comments

Comments
 (0)