Call javaScript Function After Whole Page Load Complete

Call javaScript Function After Whole Page Load Complete

Call/Execute javascript function after the whole web page is loaded. Here you will learn two ways to call javascript function after the whole web page is loaded.

When you work with jQuery/javascript, sometime you need to load whole web page and after that call/execute javascript functions.

1. First Simple Way

You also know that in HTML <body> tag holds the actual content that is used to display to the users. Using the onload with HTML <body> tag, you can execute the javascript function after the whole page is loaded. The onload event occurs whenever the element has finished loading.

You can do like:

<body onload="functionName">
<!DOCTYPE html> 
<html> 

<head> 
	<title> Execute Javascript Function After Page Load Example </title> 
</head> 

<body onload="afterPageLoad()"> 
	
	<h1>Welcome to tutsmake.com</h1> 
	
	<p> Execute Javascript Function After Page Load Example </p> 
</body> 

<script language='javascript'>
function afterPageLoad(){
   alert('hello');
}
</script>

</html> 

2. Second Way

You can use the javascript window onload property. Which is used to call/execute javascript functions after the whole page is completely loaded.

You can do like:

window.onload = function afterWebPageLoad() { 
  
    // Function to be executed 
} 
<!DOCTYPE html> 
<html> 

<head> 
	<title> Execute Javascript Function After Page Load Example </title> 
</head> 

<body> 
	
	<h1>Welcome to tutsmake.com</h1> 
	
	<p> Execute Javascript Function After Page Load Example </p> 
</body> 

<script language='javascript'>
function afterPageLoad(){
   alert('hello');
}

window.onload = function afterPageLoad();

</script>

</html> 

Recommended JavaScript Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *