How do I call a JavaScript function on page load

0 votes

To call a JavaScript function once the page has loaded, you'd add an onload attribute to the body containing a bit of JavaScript (usually only calling a function)

<body onload="foo()">

When the page has loaded, I want to run some JavaScript code to dynamically populate portions of the page with data from the server. I can't use the onload attribute since I'm using JSP fragments, which have no body element I can add an attribute to.

Is there any other way to call a JavaScript function on load? I'd rather not use jQuery as I'm not very familiar with it.

Sep 4, 2020 in Java-Script by kartik
• 37,520 points
22,926 views

1 answer to this question.

0 votes

Hello @kartik,

If you want the onload method to take parameters, you can do something similar to this:

window.onload = function() {
  yourFunction(param1, param2);
};

This binds onload to an anonymous function, that when invoked, will run your desired function, with whatever parameters you give it. And, of course, you can run more than one function from inside the anonymous function.

Hope it helps!!

If you need to know more about Java, join our Java online course today.

Thank You!!

answered Sep 4, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
1 answer

How do I remove a property from a JavaScript object?

Hello @kartik, The delete operator is used to remove properties ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,840 points
902 views
0 votes
1 answer

How do I include a JavaScript file in another JavaScript file?

Hello @kartik, It is possible to dynamically generate ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,840 points
835 views
0 votes
1 answer

How do I send a cross-domain POST request via JavaScript?

Hello @kartik, Follow this steps: Create an iFrame, put a ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,840 points
1,766 views
0 votes
1 answer

How do I add arbitrary html attributes to input fields on a form?

Hello @kartik, If you are using ModelForm, apart from ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,840 points
926 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,994 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,691 views
0 votes
1 answer

How do I remove a property from a JavaScript object?

Objects in JavaScript can be thought of ...READ MORE

answered Jun 8, 2020 in Java-Script by Niroj
• 82,840 points
1,460 views
0 votes
1 answer

How do I run PHP code when a user clicks on a link?

Hello @kartik, You'd need to have a javascript ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,840 points
9,864 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP