Why we import CSS & JavaScript files in HTML like this ?

Why we import CSS & JavaScript files in HTML like this ?

I always wonder?why we do, what we do?and sometimes I try to figure out few things in detail.?JavaScript?is always special when it comes to explore new things. This blog is simply an outcome of my?trial and error methods?when I was playing with the HTML tags. So…let’s get started !

I am sure you might come across this simple template whenever you start working on any new project.

Starting template of HTML file

As soon as we get this, we?simply add?styles.css?in meta tag (line 7 in below image) and?app.js?just before the closing of body tag (line 14).

final updated template - CSS & JS file added

But have you ever thought why we even do exactly this and not opposite or not any other way..? well, this blog will answer all such questions.

  1. Firstly we will try?Inline JavaScript?(see line no. 9 below ) where we introduced?onload?and set an alert.

Inline JS

Just like Inline CSS, Inline JavaScript also has few downsides. It’s?not so modular?, it is very?difficult to debug?so it is advised not to practice it.

2.?Internal JavaScript?is one another method to write JS. Here JavaScript is written inside script tag (see line no. 11 to 15 below)

No alt text provided for this image


But it’s not so cleaver way to write JS in HTML file as it create clutter and it may lead to ambiguity while debugging the code or updating existing file. so we use -

3. External JavaScript ,?It’s most commonly used method as it separates everything and makes easy to debug. For this, we create a file with?.js?extension eg.?app.js?and simply link this to our main HTML file. (see line no 13 of index.html below) and write our entire JavaScript code in it.

External JS

Now, Magic starts… Hang on

The position of script tag and where we link CSS inside the HTML document plays very important role in loading our web page.

let's check HOW ?

Case A)

When we place CSS stylesheet at bottom — before the end of body tag

CSS at the end

The code runs top to bottom, line by line and when it reach to <h1> tag and prints the heading with normal black text (browser prints “Hello” here) and then script tag hits and we get an alert showing some message.

If we dismiss this alert then it goes to next line where we linked CSS file, which then gets executed and changes the color of text from black to red.?This entire thing is not what we intended to execute, but it happens because of importing external files at the wrong place.

So, we always put CSS file at the top?(usually below the title)

Case B)

When we place JavaScript file at top - in Meta tag

JS in meta tag

See line no. 7, here execution context hits?script?tag first while executing, So it look inside JavaScript file (app.js?). In JavaScript code we are just finding <h1> and changing it’s text from “Hello” to “Bye”.

But at this stage it is not able to find <h1> tag, So we get an error in console —?cannot set property ‘innerHTML’ of ‘null.’ (check error message in red lines at top extreme left of above image)

which means <h1> isn’t exists yet. The reason is : execution is happening top to bottom , line by line. We are on line 7 of HTML and <h1> tag is below it (on line 12) so it returns?null.

…So, the best practice is to?put script at the end and just before closing of </body> tag.

The benefit of a separate file is that the browser will download it and store it in its cache. Other pages that reference the same script will take it from the cache instead of downloading it again, so the file is actually downloaded only once. That reduces traffic.

  • By doing this, all the content of our website gets loaded first which in turn makes our website load faster.

If you found this article helpful, do let me know !

you can also read my previous article here

Thanks for reading !



要查看或添加评论,请登录

Vaibhav Matere的更多文章

  • Demystifying NPM

    Demystifying NPM

    When you are building a software product or a service solution, then you will not always build everything from scratch;…

    1 条评论
  • The rise and rise of JavaScript

    The rise and rise of JavaScript

    The story starts from 1995, The Netscape Navigator browser was very popular and getting very high adoption from…

    6 条评论
  • What is Hacktoberfest & Why should we care about it ?

    What is Hacktoberfest & Why should we care about it ?

    Hactoberfest in Open Source initiative by Digital Ocean organized to encourage developers to contribute, participate to…

    1 条评论
  • Power Of Connections and Networking

    Power Of Connections and Networking

    By building up a network of people around you who have similar passion and are headed in the same direction, you…

社区洞察

其他会员也浏览了