How to call a function ?

How to call a function ?

to the commission of any events on the site in your browser, there are certain repetitive actions, you need to use the object code - function.Functions can replace a bulky, repetitive portions of code.We define the function once, you can play it in different parts of the code several times.In this article, we describe in detail about how to call a function in JavaScript.


code Before you call a function, we must first define it.Function, like any other variable, must take a certain value, which in this case is defined by the user.

function is an expression that consists of the following components:

  1. key keyword function.
  2. Options.As the function may include certain parameters, and without them.
  3. body containing instructions that you must follow.It is always enclosed in braces.

Schematically it can be displayed as:

  • new Function ([arg1 [null, arg2 [null, ... argN]],] functionBody)

example and call options function in JS

Consider the simple example of a function.Suppose we want

to every time after the user has passed the registration process on our website, before it pop up a modal window with the message "Welcome to our website!".To do this in the appropriate section of the code, we need to register the following command:

  • function showMessage () {
  • alert ( 'Welcome to our site!');
  • }

Call function is always associated with some object on the page, or acts committed on him.

  • function to fire before the page is fully loaded in the browser, it must be placed in the header portion (& lt; head & gt; ... & lt; / head & gt;).
  • order shall come into effect after the page is fully loaded site, the call must be placed in the tag body (& lt; body & gt; ... & lt; / body & gt;) using the onLoad event.
  • To function was called after every mouse click in any area of ​​a document, use the event onClick body tag (& lt; body onclick = "showMessage ()" & gt;).
  • To fire the function of clicking on a certain area of ​​the document, before the event onClick should be placed on the sending of a block element (eg, div: & lt; div onclick = "showMessage ()" & gt;).

Do not forget that all the tags in the HTML must be closed, otherwise an error occurs in the code.

More useful information can be found in the section Computers and ICT website.