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 defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
JavaScript Syntax

Last accessed pages

  1. PHP MySQL - WHERE and LIKE (29321)
  2. PHP Unzipper - Extract Zip, Rar Archives (31737)
  3. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (55416)
  4. SHA1 Encrypt data in JavaScript (35321)
  5. Output or Force Download MP3 with PHP (5663)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (250)
  2. Read Excel file data in PHP - PhpExcelReader (90)
  3. PHP Unzipper - Extract Zip, Rar Archives (76)
  4. The Four Agreements (73)
  5. The Mastery of Love (66)
Chat
Chat or leave a message for the other users
Full screenInchide