Javascript Course

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>
<html>
<head>
<title>Title</title>
</head>
<body>
<h4>Example simple JavaScript code</h4>

<script>
document.write('Text from JavaScript displayed in web page.');
</script>
</body>
</html>
- 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>
<html>
<head>
<title>Title</title>
</head>
<body>
<h4>Example simple JS code</h4>

<script src='code.js'></script>
</body>
</html>
- 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 \ marplo.net), 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

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
JavaScript Syntax

Last accessed pages

  1. Multidimensional arrays and array functions (8646)
  2. CSS Code Snippets (687)
  3. JavaScript Syntax (2844)
  4. JavaScript Course - Free lessons (31376)
  5. Validate radio and checkbox buttons (9980)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (253)
  2. Read Excel file data in PHP - PhpExcelReader (91)
  3. The Four Agreements (77)
  4. PHP Unzipper - Extract Zip, Rar Archives (77)
  5. The Mastery of Love (66)