02. Applying JavaScript

Applying JavaScript is easy - we can apply JavaScript both internally and externally.

Internal JavaScript

JavaScript code can be placed in between the <script> tag either the head or body elements of an HTML document.

<script>
  document.write("Hello world!");
</script> 

Here, we're writing the text "Hello world!" to the document. We'll learn all about the document object and its functions in the Document Object Model section.

If you plan on placeing JS code directly in your body, it's best to place it just before your </body> closing tag. This will improve page loading time since no HTML will be blocked by any of your scripts loading.

External JavaScript

It's best to keep your HTML clear of JavaScript code for readability and maintanability. An external .js file may be cached (saved on a user's computer for quick access), which can speed up page load times.

To link externally, make your external file's extension .js and link it to your document with the src attribute:

<script src="path/to/js/script.js"></script>

Build modern and responsive webpages

JavaScript & JQuery

Build modern and responsive webpages Try Front-end

This book was written for anyone who wants to use JavaScript to make their websites a little more interesting, engaging, interactive, or usable. In particular, it is aimed at people who do not have a degree in computer science (well, not yet anyway).

$ Check price
39.9939.99Amazon 4.5 logo(385+ reviews)

More Front-end resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Ad