Instruction
1
If the function needs to work before the end of the page load in a browser, then it should be placed in the header part of the source code between the tags <head> and </head>. If the code of the function itself is also placed in the header, the line with its call may immediately follow the closing parenthesis of the function. Test page with this variant of the function call may look like this:<html>
the <head>
the <script>
function testFunction() {
alert('Function worked!')
}testFunction()
</script>
</head><body></body></html>
2
If the function should work after downloading the body of the document, its call should be put in the body tag using the onLoad event. For example:<body onload="testFunction()">
3
If the specified function should be called each time you click in the body of the document, then you need to use the onClick event of the same body tag:<body onclick="testFunction()">
4
If you need to call a function on click in a certain area of the page, it is possible to put, for example, a block element DIV and catch its onClick event:<DIV onclick="testFunction()">DIV</DIV>
5
A mouse click can be used as an excuse to call the function, almost every page element. With image:<img src="pic.gif" onclick="testFunction()" />button:<button onclick="testFunction()">button</button>With a text entry field:<input TYPE="text" onclick="testFunction()" />link:<a href="/" onclick="testFunction();return false">link</a>
6
To call the function by clicking on the link you can use another syntax is to place the appropriate JavaScript code in the href attribute. For example:<a href="javascript: testFunction();">link</a>
7
To any of these events, you can add a delay function call, if you use the method setTimeout. For example, if you want to call the function two minutes after the page load, then you can write the body tag:<body onload="setTimeout('testFunction()', 120000);">Here the time is specified in milliseconds, i.e. one thousand is one second.