Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Sunday, 15 March 2015

Javascript Output

JavaScript does not have any built-in print or display functions.

JavaScript Display Possibilities

JavaScript can "display" data in different ways:
  • Writing into an alert box, using window.alert().
  • Writing into the HTML output using document.write().
  • Writing into an HTML element, using innerHTML.
  • Writing into the browser console, using console.log().

Using window.alert()

You can use an alert box to display data:.

Using document.write()

For testing purposes, it is convenient to use document.write():

Using innerHTML

To access an HTML element, JavaScript can use the document.getElementById(id) method.
The id attribute defines the HTML element. The innerHTML property defines the HTML content:

Using console.log()

In your browser, you can use the console.log() method to display data.
Activate the browser console with F12, and select "Console" in the menu.




What is Javascript

JavaScript is the programming language of the Web.
All modern HTML pages are using JavaScript.

 JavaScript Can Change HTML Content
One of many HTML methods is document.getElementById().
This example "finds" the HTML element with id="demo", and changes its content (innerHTML):

Example

document.getElementById("demo").innerHTML = "Hello JavaScript";

JavaScript Can Change HTML Styles (CSS)

Changing the style of an HTML element, is a variant of changing an HTML attribute:

Example

document.getElementById("demo").style.fontSize = "25px";
JavaScript can be placed in the <body> and the <head> sections of an HTML page.

The <script> Tag

In HTML, JavaScript code must be inserted between <script> and </script> tags.

Example

<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>

JavaScript Functions and Events

A JavaScript function is a block of JavaScript code, that can be executed when "asked" for.
For example, a function can be executed when an event occurs, like when the user clicks a button.
You will learn much more about functions and events in later chapters.

JavaScript in <head> or <body>

You can place any number of scripts in an HTML document.
Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both.

External JavaScript

Scripts can also be placed in external files.
External scripts are practical when the same code is used in many different web pages.
JavaScript files have the file extension .js.
To use an external script, put the name of the script file in the src (source) attribute of the <script> tag: