Skip to main content

How to insert JavaScript into web pages

You have learned everything there is to know about HTML and CSS to start building your static website and now you would like to make it dynamic but you don't know how to insert JavaScript into web pages; right? Don't worry! Inserting the scripts into html files is really a breeze. If you read this article, in the next five minutes you will discover all the methods for inserting JavaScript into html files.

How to insert JavaScript into HTML file

JavaScript need HTML file to work. Like CSS all methods of inserting JavaScript into a Web Page can be divided into two sections; add the scripts inside the TAG of  HTML file or by calling them externally. In detail we can insert JavaScript:
  • Directly into Body of the HTML file.
  • In the Head Tag and recall it in the body.
  • With a relative or absolute external reference.

Insert JavaScript into HTML file

We will see an example of how to insert JavaScript shortly but first let's focus on when it is useful to do an explicit or implicit insertion of the code in our HTML file. Adding JavaScript explicitly is useful when working on single files; in the sense that code should not be repeated on all pages of the site.
Let's take a practical example using the JavaScript "onclick" event by changing the text within a paragraph when a user presses the button by inserting all the function code in the Head and calling it in the Body
function myFunction1(){
document.getElementById("example1").innerHTML = "Function in HEAD and recall in the Body";
}

Now if you want to hook the function; insert the following code in the body of the HTML file; precisely between the opening and closing tags of the body.

<p id="example1">First example</p>

<button type="button" onclick="myFunction1()">Recall the Function</button> 

How to insert JavaScript into web pages
How to insert JavaScript into web pages


That we have just seen can also be assembled in a single block by inserting it in any point of the file html inside the body.

Invoke external JavaScript

In large projects it is useful to separate the development logic when you need to use the same functions in multiple Web pages; you avoid rewriting all the code and call a certain function there where it is needed. A practical example is the call to external libraries including the link in the <head> </head> tags of the HTML file; or with a relative link "src" path when the recalled code resides on the same site.

How to invoke JavaScript into web pages
How to invoke JavaScript into a web pages

This guide on how to insert JavaScript code into a web page ends here! At this point you can move on to the next step to start testing your scripts; don't forget to include the code inside the opening script tags and closing script tag have fun.

Comments

Popular posts from this blog

Breadcrumbs in Notable Blogger theme

The modern themes available in Blogger are suitable for mobile devices; this is important in terms of indexing but Blogger not support breadcrumb of schema.org by default. If you add a few lines of code you can greatly improve your SEO and therefore your ranking in Google Search. I create a personal breadcrumb solution; if you have installed a responsive theme like mine in this article I explain how to add Breadcrumbs in Notable Blogger theme. How to add breadcrumbs in Notable Blogger theme First thing create a backup of you theme for security reasons You can add breadcrumbs in JSON-LD format or as following micro-data solution: Open your Blogger Dashboard Click on Theme >> Download a Backup for security reason Copy this code  <div class='post-body-container'>   Click Ctr+F and past the copied code then click Enter You will find this tag 1 time. Breadcrumbs in Notable Blogger theme Now copy the following code and past it below the tag : <b:if cond='data:view.

Object cache problem of the AMP plugin for WordPress

If you are looking for one solution to the fix object cache problem of the WordPress AMP plugin displayed in dashboard from Site health; in this article I will show You how to do this in five minutes. Usually this situation arises when the installed theme is non an official theme and You have not cache plugin installed; anyway don't worry! We will solve the problem in a moment. Object cache problem of the AMP plugin for WordPress Why is important to enable persistent object cache for AMP  Accelerate mobile page (AMP), is latest technology of the Web. An amp page load very fast compared to to a standard Web page and Google search engine like very much this; so you can improve the ranking of your site and get more mobile users traffic. Persistent object cache If persistent object cache is enabled, WordPress work better; the images and files as CSS will loaded more fast. Every WordPress Hosting have a cache service, otherwise it would be very strange and I recommend that you consider

CMS Content Management System

Anyone who wishes to construct and administer a website without knowing how to code should consider using content management systems (CMS). A content management system (CMS) is a piece of software that makes it simple to add and alter material to websites without changing the underlying code.  Because of this, CMSs are a fantastic option for organizations and people who lack the technical know-how to build a website from scratch.  CMS  How many types of CMS exist? There are several available CMSs, each with unique benefits and drawbacks. The most well-liked CMSs include: WordPress Drupal  Joomla  Magento  Shopify  TYPO3 Picking the CMS that best meets your needs is vital since each one has unique features and functions. Gins that are offered. If You learn PHP, Javascript, HTML and CSS programming language You can create Your personal CMS. Who can use a CMS?  Anyone who wants to build and administer a website without knowing how to code will find CMSs to be an excellent option. You may