Adding JavaScript in an HTML page

To insert JavaScript into an existing HTML document, it is necessary to introduce the <script> </script> tag. Inside the <script> ... </script> element we write our JS code.

To write and execute JavaScript programs we need a simple text editor (such as Windows Notepad, emacs Unix or Notepad++) and a browser (Mozilla Firefox, Chrome, Internet Explorer).

We can also place the JS instructions in a separate JavaScript file, external, that will have the '.js' extension. To edit this file is needed only a simple text editor, like Notepad. The advantage of an external file is that we can use the same code in multiple HTML pages in case of a necessary change in the JavaScript code, we just need to modify the data in a single file (the one with the extension 'js').
In the '.js' external file we can't use HTML tags.

Here is a sample JavaScript script written within a web page (HTML):
<!DOCTYPE html>
<h4>Example simple JavaScript code</h4>

document.write('Text from JavaScript displayed in web page.');
- The document.write instruction is used to print something on the page.

• If you want to load the script from an external file (eg, 'code.js '), our code in the HTML document will look like:
<!DOCTYPE html>
<h4>Example simple JS code</h4>

<script src='code.js'></script>
- The 'code.js' contains:
document.write('Text from JavaScript displayed in web page.');

Syntax conventions

In any language, the code has conventions and rules of syntax.
Now it's presented the JavaScript syntax rules:

  1. Case-sensitive - It makes distinction between large and small letters, such words as 'examples, Examples' will be treated differently.
  2. Semicolon (;) - All line statements must end with a semicolon character (;) (Example' var1 = 3; var2 = 8;).
  3. Blank spaces - JavaScript ignores blank spaces, tabs and blank lines that appear in the instructions, they are useful for making code more structured and easy to read. Admit only the spaces that appear in strings.
    - Example:
    var1 = 'JS tutorial';
    //is the same as
    var1='JS tutorial';
  4. Quotes - The single quotes ('') and double quotes (" ") are used to delimit strings. (Example: 'I learn JavaScript' or "I learn JavaScript").
  5. Special Characters - When we write scripts, sometimes we need to use a special character in values or in data output, or a key press such as the TAB key, or a new line. In that case we must use the backslash character '\' in front of one of the Escape codes:
    • \b - backspace
      \f - new page
      \n - new line
      \r - indicate a carriage return
      \t - indicate a pressing TAB key
      \\ - a backslash character
      \' - indicate an apostrophe (single quotes)
      \" - double quotes
    • - For example, if you want to display a text using document.write(), and that text should include quotes and backslash character '\', like ('JavaScript' Course \, to not confuse the script in code interpretation, becouse the quotes and backslash are part of the syntax, we add \ in front of these characters within the string. The command for display the string will be:
      document.write ('\' JavaScript \' Course \\ and tutorials.');
  6. Comments - The comments within the code are necessary when we want to specify the role and functions of certain variables or instructions, for easy understanding of the script later.
    To add a comment on a single line, inside the code, use double slash //. All characters to the right of the double slash represent a comment.
    - Example:
    // Comment on a single line
    var str ='String';
    If you want to write comments on multiple lines, use /* at the beginning of the comment, and */ at its end.
    - Example:
    /* comment on ...
    multiple lineas
     end line */
    var str ='String';
  7. The name of the variables and functions - The name given to variables and functions must follow the following rules:
    • - The first character must be a letter, an underscore (_) or $ sign.
      - The first character can not be a number.
      - the name should not contain blank space.
      - Do not use reserved words that are part of the JavaScript language (such as 'array', 'status', 'alert'), because the interpreter program will not make the difference between these names and JavaScript commands with the same name.

Daily Test with Code Example

Which HTML5 tag defines marked text? (can be used to highlight parts of text)
<mark> <embed> <span>
<p>Free corses: <mark></mark> for Web Development.</p>
Which CSS pseudo-class adds a style to an element when the mouse is over it?
:focus :hover :active
a:hover {
  font-weight: bold;
  color: #00da01;
Click on the function which returns a string value that represents the number rounded to the x digits after the decimal point.
toPrecision(x) toFixed(x) floor(x)
var num = 12.34567;
num = num.toFixed(2);
alert(num);       // 12.35
Indicate the PHP function which reads an entire file into an array.
[) file() readfile()
$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
JavaScript Syntax

Last accessed pages

  1. CSS cursor property - Custom Cursors (4008)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (17701)
  3. Animating in Flash - Frame-by-Frame Animation (2031)
  4. JpGraph - Create Graph, Charts, Plots in PHP (1321)
  5. Contact page - CoursesWeb (21725)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1270)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (1241)
  3. Tabs effect with CSS (1227)
  4. Contact page - CoursesWeb (1225)
  5. Insert, Select and Update NULL value in MySQL (729)