Your first Javascript File

0
907

Hello everybody, and welcome to Hacoder. Today, we are going to create our First Javascript File.

If you have no prior knowledge of Javascript and still haven’t checked out our previous Javascript Tutorials, then click here.

Alright, let’s create our First Javascript File.

Your First Javascript File:-

In this tutorial, we are going to cover:

  • How to embed Javascript code into the HTML webpage.
  • How to reference your JavaScript from a separate file.
  • How to create your first Javascript powered webpage.

Firstly, we have to create a sample HTML webpage.

Save it as index.html or with any name but with a .html extension.

The JavaScript script is inserted either in the HTML page itself or in a separate file. Firstly lets take a look how Javascript is inserted into HTML webpage.

How to Embed Javascript code into HTML webpage:-

To insert a JavaScript script in an HTML page, you use the <script> … </script> tag. Don’t forget the closing </script> tag! All the Javascript code is inside the <script> </script> tag.

We can write the <script> </script> tags either in the <head> section or just before the closing of </body> tag.

We can write the Javascript code inside the <head> section like this:

And we can write the Javascript code just before the </body> tag like this:

Save this as index.html and run it on your Web Browser, by just double clicking on the file.

Webster’s advice:

TO INCREASE THE PAGE SPEED OF YOUR WEBPAGE, YOU SHOULD INSERT YOUR JAVASCRIPT CODE JUST BEFORE THE CLOSING OF </BODY> TAG. THIS WILL LOAD THE JAVASCRIPT CODE AFTER ALL THE CONTENT OF THE WEBPAGE IS LOADED, WHICH WILL INCREASE THE PAGE SPEED.

Reference your JavaScript from a separate file:-

If your script is longer than a few lines, or you need to apply the same code to several pages in your website, then packaging your JavaScript code into a separate file is your best bet.

In fact, having your JavaScript in its own file will give you the advantage of easily maintaining and reusing your scripts.

Here’s how it’s done:

Here, we have referenced our <script> tag to an external Javascript file myjsfile.js . Remember, external Javascript files are saved with .js extension.

Creating your first Javascript powered webpage:-

So now, we are going to create our First Javascript Powered Webpage. Remember we can do it in two ways: By Embedding the Javascript code into HTML code, and By Referencing to an External Javascript File.

By Embedding the Javascript code into HTML code:-

Run the index.html in your web browser and see the output.

By Referencing to an External Javascript File:-

Save this above code as index.html and now let’s create our External Javascript filemyjsfile.js .

Now, open the index.html file in the Web Browser and see the output. It should alertHello, World! in the Web Browser.

That’s it for now. Lots of Javascript tutorials are coming up soon on Hacoder.


Any questions, comments or suggestions are welcomed.

NO COMMENTS

LEAVE A REPLY