how to call javascript function in html & JavaScript Define

In how to call javascript function in html, JavaScript is a programming language commonly used in single page web applicaiton to perform some js methods or pure js based functions that the HTML DOM cannot do. It can be help for get a really and tru data for validating forms, and accessing your browsers data, including the dynamic any run time Implements statement, module and more!

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

What is Function in JavaScript?

In example how to call javascript function in html, JS Functions are Main imp part as well as helfup in any devlop programming language like as a js, php or any client side as they make the scripts reusable A User-defined function is a block of scripts which will be run ot executed only if it is called. If you have a some source code lines of scripts that requires to be used more times, you can make a User-defined function added the repeating some basic lines of scripts and then this js call the User-defined function wherever you want.

How to Create a Function in JavaScript

  • Use the keyword function followed by the name of the User-defined function.
  • Now the User-defined function name, open as well as close parentheses.
  • Now parenthesis, open as well as close your function curly braces.
  • Within curly braces, write your lines of scripts.

Syntax:

function callJsUserFunc()
{

  lines of source scripts to be run or executed

}

Example

<html>
<head>
	<title>Functions!!! - call javascript function from html</title>
	<script type="text/javascript">
      function callJsUserFunc()
      {
      	document.write("This is a simple function.<br />");
      }
		callJsUserFunc();
	</script>
</head>
<body>
</body>
</html> 

Function with Arguments

You can make a simple JS functions with Parameters as well. Parameters should be specified within parenthesis

Syntax:

//call javascript function from html
function callJsUserFunc(param1, param2)
{

  lines of source scripts to be run or executed

}

Example: call javascript function from html

<html>
<head>
<title>call javascript function from html</title>
	<script type="text/javascript">
		var count = 0;
		function countVowels(user_info)
		{
			for (var i=0;i<user_info.length;i++)
			{
              if(user_info[i] == "a" || user_info[i] == "e" || user_info[i] == "i" || user_info[i] == "o" || user_info[i] == "u")
              count = count + 1;
			}
		document.write("Grettings " + user_info + "!!! Your user_info has " + count + " vowels.");
		}
   	 	var userInformation = prompt("Please enter your Good User Information");
    	countVowels(userInformation);
	</script>
</head>
<body>
</body>
</html> 

JavaScript Return Value

You can also make a JS functions that data return values. Inside the JavaScript User-defined function, you required to use the main keyword return bellow by the value to be returned.

Syntax:

function callJsUserFunc(param1, param2)
{

  lines of source scripts to be run or executed

  return val1;

}

Example : call javascript function from html

<html>
<head>
	<script type="text/javascript">
		function totalCountOfData(beging, lastdata)
        {
          var total = beging + lastdata;
          return total;
        }
      var beginingPersonId = 89;
      var lastPersonId = 65;
      document.write(beginingPersonId + " + " + lastPersonId + " = " + totalCountOfData(beginingPersonId,lastPersonId));
	</script>
</head>
<body>
</body>
</html>

how to call javascript function in html?

It is beyond the scope of this guide to teach you JavaScript, but below you can learn how to embed or integrate JavaScripts into your website using the

In JavaScript functions, the example here stands to the this, is the main js data object that "owns" the active DOM source scripts. The Data value of this, when used in a User-defined function, is the main object that useful the "owns" the method.

this in a Method

inroWithFlname : function() {
  return this.employeefName + " " + this.employeelName;
}

this Alone

var varName = this;

//Example

"use strict";
var varName = this;

this in a Function (Default)

function callJsUserFunc() {
  return this;
}

this in a Function (Strict)

"use strict";
function callJsUserFunc() {
  return this;
}

this in Event Handlers

<button onclick="this.style.display='none'">
  Click to Delete Me!
</button>

Object Method Binding

var employee = {
  employeefName  : "Trump",
  employeelName   : "Donald",
  id         : 9565,
  callJsUserFunc : function() {
    return this;
  }
};

Explicit Function Binding

var employee1 = {
  inroWithFlname: function() {
    return this.employeefName + " " + this.employeelName;
  }
}
var employee2 = {
  employeefName:"Trump",
  employeelName: "Donald",
}
employee1.inroWithFlname.call(employee2);  // Will return "Trump Donald"

The Global Object

When a js User-defined function is called without an owner simple object, the data value of this becomes the main this related global object.In a your web browser DOM the global object is the your any browser window. This user define functions returns the window object as the value of this:

var x = callJsUserFunc();            // x will be the window object

function callJsUserFunc() {
  return this;
}

Invoking a Function as a Method

var pmObjData = {
  employeefName:"Trump",
  employeelName: "Donald",
  inroWithFlname: function () {
    return this.employeefName + " " + this.employeelName;
  }
}
pmObjData.inroWithFlname();         // Will return "Trump Donald"

Invoking a Function with a Function Constructor

// This is a function constructor:
function callJsUserFunc(param1, param2) {
  this.employeefName = param1;
  this.employeelName  = param2;
}

// This makes a new object
var x = new callJsUserFunc("Trump", "Donald");
x.employeefName;                             // Will return "Trump"

JavaScript Function Call

Method Reuse

With simple js the JavaScript Function call() method, you can helpful a function that can be used on other some different types of the objects.
All Functions are Methods

var employee = {
  employeefName:"Trump",
  employeelName: "Donald",
  inroWithFlname: function () {
    return this.employeefName + " " + this.employeelName;
  }
}
employee.inroWithFlname();   // Will return "Trump Donald"

The JavaScript call() Method

var employee = {
  inroWithFlname: function() {
    return this.employeefName + " " + this.employeelName;
  }
}
var employee1 = {
  employeefName:"Trump",
  employeelName: "Donald"
}
var employee2 = {
  employeefName:"Mary",
  employeelName: "Donald"
}
employee.inroWithFlname.call(employee1);  // Will return "Trump Donald"

The call() Method with Arguments

var employee = {
  inroWithFlname: function(city, country) {
    return this.employeefName + " " + this.employeelName + "," + city + "," + country;
  }
}
var employee1 = {
  employeefName:"Trump",
  employeelName: "Donald"
}
employee.inroWithFlname.call(employee1, "Surat", "India");

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about how to call javascript function in html?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Laravel Left join get last record of right table in MYSQL
  2.   Laravel 6 group by month/year using Eloquent
  3.   VueJS Accessing index using v-for loop
  4.   Helper classess in laravel 6 Example - PHP
  5.   Autocomplete Textbox with Multiple Values using jQuery PHP and MySQL
  6.   Angular 6 Image Upload with Validation Example
  7.   Download Remote File via PHP CURL
  8.   how to use php in javascript? - Example | php code in javascript
  9.   Laravel Calculate multiple totals in conditional aggregates
  10.   PHP Jquery Ajax CRUD Example Tutorial From Scratch