Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

The Basics of Implementing JavaScript to Web Pages

DZone's Guide to

The Basics of Implementing JavaScript to Web Pages

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

JavaScript can be typed in any text editor like notepad and can be stored separately with the file extension.js. Usually it is stored as a part of an HTML document. JavaScript codes are included in HTML documents by using the <script> tag.

The script tag is a non-empty tag and should be closed using the </script> tag. The script tag takes in two attributes namely language and src. The language attribute specifies the name of the scripting language used like JavaScript and VBScript. The src attribute specifies the name of the source file where the script is stored. The src is used only when the scripts are stored in a separate file. <script language = “ “ src = “ “ >


JavaScript codes goes here </script>

The document.write () method is used write output to the present document which would be explained when dealing with the document object. The following example illustrates the use of script tag to embed JavaScript.

Code -

  <html>

<head>

<title> My first JavaScript program </title>

<script language = “JavaScript”>

document. write (“Welcome to JavaScript Style”)

</script>

</head>

</html>

The script tag can be placed in between the <head> tag or the body tag. Normally the script tag is placed in between the head tag. To import scripting codes into your HTML document use the src tag. The following example illustrates the use of src tag :

Document. write(“Welcome to JavaScript Style”)

 Save this code in a source file called script .js. This file is imported into HTML document in the following manner. The SRC attribute can take an URL as its attribute value. The web server that provides the source file , should report the file’s MIME type as application / x-javascript.

Code -

<html>

<head>

<title> My first JavaScript program </title>

<script language = “JavaScript” src = “script.js”>

Document. write (“Welcome to JavaScript Style”)

</script>

</head>

</html>

 Some browsers do not support JavaScript. To avoid errors when executing web pages that do not support JavaScript, the script is written within the HTML comment tags.

<script>

<!-

Javascript code

->

</script>

<html>

<head>

<title> My first JavaScript program </title>

<script language = “JavaScript”>

< !-

Document. write (“Welcome to JavaScript Style”)

//->

</script>

</head>

</html>

The JavaScript coding can be tested by loading the HTML document in the browser. Type the entire file path in the location of the browser or choose from the File menu.

JavaScript has the provision to give comment statements. To include a comment line just adds two forward slashes before the line:

// This is a comment line.

To add multi line comment include the lines in between /* and */.

/* This

is

a multi line

comment */

<html>

<head>

<title> My first JavaScript program </title>

<script language = “JavaScript”>

< !-

// This is a single line comment

/* Multiple

lines of comment */

document. write (Welcome to JavaScript Style”>

// ->

</script>

</head>

</html>

Source :
JavaScript Style

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}