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> 

You may like

  1. JavaScript Create & Call Functions with Example
  2. JavaScript Operators With Examples
  3. String Contains JavaScript | String includes() Method
  4. JavaScript Replace String With Examples
  5. JavaScript Array splice() Method By Example
  6. JavaScript: Clone an Array & Object By Examples
  7. Sorting Using JavaScript Array Sort() Method
  8. JavaScript: Convert String to Array JavaScript
  9. javaScript Push() Array By Example
  10. javaScript Push Array, Items Into Array Example
  11. Check the Object is Array or not in javascript
  12. JavaScript: Important Array Methods
  13. Highlight Search Result Vanilla JavaScript

Leave a Reply

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