Adding scripts to a web page

When you have a web page, some of the functionality you see online is achieved through the use of scripts that are small sets of instructions, which will execute during the loading of the web page.

Scripts can be saved as part of the web page or as separate files. If you save them as separate files, you will likely give them a .js extension. You can also be embedding them onto the web page source code.

In any case, you will use the <script></script> code to insert it on your site. If using a separate file, you include a src="" modifier to the instruction to state where the file is. If you embed the code directly, you use the comment code to point where the script starts and end.


<script src="myscript.js"></script>

my script instructions

Tips about scripts

Although inserting scripts on a web page is pretty straightforward; still, there are a couple of things I think you should know about them.

The first one is that scripts will stop the loading of your web page until the computer finishes its processing. Therefore, you should not abuse scripts. Add the ones you strictly need, and if possible, send them to the bottom of your web page. That way, most of the site will have loaded when they start processing.

The best way to add the scripts you are willing to use is saving them under separate files. Thus, for a script "A" written in Javascript, the best thing to do is to create a file "a.js" and include all the code there, so that you can only call that file when needed on the final page.

There are two advantages of using this method. The first one is that you will not have to change your files one by one if you ever need to change the script. The second one is that your website will consume less server resources and help you have a better performance.

Lastly, scripts can go both on the <head></head>; or <body></body> section of your web page. Mind, it is not "ad lib" but "as needed".

Learn + Computers and the Internet + Web development