About JavaScript code, examples

JavaScript is an interpreted, object-oriented language and is known as the scripting language for web pages.

How to add javascript code in HTML?

JavaScript uses the Unicode character set. Unicode covers almost all the characters, punctuations or symbols. In HTML pages, JavaScript code must be placed between <script> and </script> tags.

alert("Hi webpedia");

There are old javascript examples that use “type attribute” inside <script> tag like:

<script type="text/javascript">
alert("Hi webpedia");

The type attribute is not required because JavaScript is the default scripting language in HTML.

Where to place JavaScript code in HTML?

The JavaScript code can be placed inside the <body> or in the <head> section of any HTML page.
To improve page load, is recommended to place the JavaScript code at the bottom of the <body> element, just before </body> tag.

The position of JavaScript code in a page does matter.
By default the code will be executed as soon as the browser sees it.

External JavaScript code

JavaScript code can also be placed in external files. This method brings us some advantages:

  • Use the same code in many different web pages.
  • Separate HTML and JavaScript code.
  • Cache the JavaScript files, which can improve the site speed.
  • Also, the JavaScript code is easier to read.

JavaScript files have the file extension.js and must be placed in <body> or <head>, inside the src (source) attribute of the <script> tags like this:

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

Make sure the external .js files does not contain <script> tags!!!

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

Leave a Reply

Your email address will not be published. Required fields are marked *