FANDOM


This article or tutorial is a work in progress and is still being worked on by other users. Give it some time to mature.

JavaScript is the premier client-side scripting language used on the Internet. It can do a multitude of things ranging from the validation of form data to the creation of complex user interfaces. JavaScript is the language for HTML, for the Internet, for computers, servers, laptops, tablets, smartphones, and more.

JavaScript is the interactive member of the triangle of Web technologies, along with HTML and CSS, and can bed used to dynamically manipulate the content, markup, and style of Web pages.

Hello JavaScript World

The first JavaScript example we will use is the standard "Hello World" example. We use JavaScript to write the string "Hello JavaScript World" into a simple HTML document:

<!DOCTYPE html>
<head>
  <meta charset="UTF-8">
  <title>Hello JavaScript World</title>
</head>
<body>
<h1>My First JavaScript</h1>
<hr />
<script>
  document.write("Hello JavaScript World!");
</script>
</body>
</html>

Take note of how the script is included directly into the markup using the <script> element, which encloses the following one-line script:

document.write("Hello JavaScript World!");

In HTML, you can copy the source code above in <script></script> elements. By doing so, the browser can differentiate between what is HTML and what is JavaScript or normal text.

Hello World Errors

Adding JavaScript to HTML Documents

There are four ways to include script in an HTML document:

  • Within the <script> element
  • Within an HTML event handler attribute such as onclick
  • As a linked file referenced using the src attribute of the <script> element
  • Using the pseudo-URL javascript: syntax within some link

The <script> Element

The primary and most commonly used method for including JavaScript within HTML or XHTML is the <script> element. A script-aware browser assumes that all text within the <script> element is to treated as some form of a scripting language; in the case with the common browsers, this scripting language is JavaScript.

<script>
  alert("This is JavaScript");
</script>

However, it is possible for the browser to support other scripting languages. For instance, the Internet Explorer family of browsers natively supports VBScript.

The language Attribute

The traditional, albeit nonstandard, way to indicate the scripting language being used is to specify the language attribute for the tag, like so:

<script language="JavaScript">
  alert("This is JavaScript");
</script>

However, what follows is an example of how to indicate that VBScript is in use:

<script language="VBS">
  msgBox "This is VBScript"
</script>

The type Attribute

However, according to the W3C HTML syntax, the language attribute is not safe. Instead, the type attribute should be set to indicate the MIME type of the language in use. Some examples of JavaScript MIME types include:

  • text/javascript
  • application/javascript
  • application/x-javascript
  • text/ecmascript
  • application/ecmascript

For safety reasons, users are to use solely the text/javascript MIME type, as it is cross-browser compatible:

<script type="text/javascript">
  alert("This is JavaScript");
</script>

However, VBScript can also be specified, as is the case here:

<script type="text/vbscript">
  Document.write("Hello!")
</script>

Using the <script> Element

Script in the <head>

Event Handlers

Linked Scripts

Linked Script Consideration

JavaScript Pseudo-URL

JavaScript Usage Considerations

Script Masking

The <noscript> Element

Language Versions

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.