javascript introduction


JavaScript syntax is a programming script, that runs on the frontend. It is mostly used for event handling like displaying or hiding some contents, replacing some contents or changing css styles depending on user interactions on a predefined UI which is termed as event handling collectively. Here are a few tasks that we can performusing javascript.

Event Handling

As stated earlier, javascript can handle events very easily. We can display, hide, or replace defined contents of the page on a predefined action performed by the user or change the css styles of the contents or display some information or prompt the user to input some information on pop up boxes with great ease.

Animations

Javascript can be used for animations too. You might have noticed some websites like Melanie Fashion, The Boat, Giampierobodino, Urban Walks which are few of the best animation websites. These effects can be created using javascript.

Browser Detection, Control and Redirect

Javascript can be used to detect the browser or its version or the screen resolution being used by a user and pages specifically designed for such browsers can be loaded instead of the default page.

Forms Validation

Javascript is most widely used for this purpose especially in static HTML webpages where server side script is not present. In other cases, it might serve as an option.

Work with Cookies

Javascript can also be used to store cookies on a user's browser and then loaded automatically the next the same user visits the site. The cookies may contain user information like username and password for the site, language, layout or color scheme perferences. Javascript can delete the cookies too.

Display Live Date and Time in Webpages

Javascript can be used to display date and time in web pages easily. Apart from the basics, we can make it run as a live clock changing every second.

Javascript syntax can be added inline to a HTML element if it's quite short but for relatively huge block of codes, you should write them inside the <script></script> markups and place it either inside the <head> markups or right above the closing </body> markup. It can be placed anywhere in the document though but that won't keep the HTML file clean and organized. Without the <script></script> markups, javascript codes will be treated as plain texts by browsers and simply displayed on the page. Here's an example of a javascript code.

<html>
  <head>
    <title>My Javascript Code</title>
  </head>
  <body>
    <script>
      alert("Hello world!!!");
    </script>
  </body>
</html> 

Alert

The example above uses alert() method which is a standard javascript function that loads a pop up box in the screen with the message added inside the parenthesis. The OK button must be clicked to close the pop up box and make browsers go through further blocks of codes.