How to Embed JavaScript to a Web Page?

02.28.2014 by Lucie Kruger

The competition in the marketplace is mounting to great heights, and thus, just a plain business website can't help you excel over your competitors. It's necessary to make your website interactive enough so that it could draw more people towards your site, and won't let your existing customers' interest fade away from your site. You can add some small programs into your web pages so as to add interactivity. These small programs are referred to as scripts, and are executed by the web browser.

JavaScript - A Foreword

JavaScript is one of the most highly coveted scripting languages for the web. JavaScript was incepted with the basic purpose of adding more stimulating features to HTML pages. Why? Well, HTML itself can be quite dreary as it cannot do much apart from displaying a web page. JavaScript, in contrast, facilitates several invigorating features like perform computations, giving user the privilege to interact with web pages, make changes to elements, adding dynamic functionality to web pages and so on. And thereby, caters better user experience.

The best thing about using JavaScript is that since it's a client-side programming language, all of its features can be accessed immediately without depending on the browser to refer to the server frequently, which eventually helps to prevent time delays and enhances user satisfaction. In this guest post you'll learn as to how you can embed your JavaScript to any web page.

Ways to Embed JavaScript to a Web Page

JavaScript mainly runs in the browser beside HTML and CSS, and can be embedded to a web page by making use of the script tag. There are two ways through which a script element can include JavaScript - either it embeds JavaScript directly within the HTML code of your web page, or otherwise, stores the scripts to some external files. Let's have the two ways using which scripts can be added to any web page:

  • Scripts in the Body

    Although, you add the scripts in different places, the first and foremost option is to add it within the HTML code in the body of your web page. Once the web page is loaded and consummated by the browser, the script will get executed. What you'll need to do is to simply put the JavaScript commands within the script tags contained by the HTML code of the body, as given below:

    Well, here I'll like to mention that you may find a "language="javascript" attribute used for the

  • External Scripts

    Another method to add JavaScript in your web page involves storing the JavaScript code to an external file and linking it from inside the HTML code. The best part is that by following this method you can make use of the same code utilized by a host of diverse web pages on your website. Additionally, placing your JavaScript code within the external file saves it from getting flagged in the form of an error as marked by the HTML validators. For an instance, scripts that consists of "<" or "/" symbols, can make the HTML validator to whine as it will attempt to read these symbols as parts of the opening and closing script tags.

    Let's use the previous Example 1: the only difference is that while using this method, the JavaScript command will get stored in the external file time.js:

    As you can see the time.js file is a simple text file, and comprises of just two JavaScript lines, and don't involves the use of the HTML tags anymore, that is, it isn't using the <script>...</script> tags! So, now in the document body we'll have:

Let's Finish It Up!

Well, if you want to add JavaScript to your web page, then it is recommended that you must read the above mentioned ways to embed your script into the web pages. However, the best way is - to add scripts in the body. How? Just alter the contents contained within the script tag and refresh the page. The change will be made - now that's pretty quick and easy!

About the author

Lucie Kruger

Lucie Kruger is a tech savvy writer who works for a leading Android Applications Development Agency - You can also contact her, if you are looking forward to Hire Android App Developers.