How do I call a JavaScript function on page load?
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.
How can I execute a JavaScript function on the first page load?
The code below will execute once the onload
event fires. The statement checks if
the onetime function has NOT been executed before by making use of a flag (hasCodeRunBefore
), which is then stored in localStorage
.
window.onload = function () {
if (localStorage.getItem("hasCodeRunBefore") === null) {
/** Your code here. **/
localStorage.setItem("hasCodeRunBefore", true);
}
}
Note: If the user clears their browsers' localStorage
by any means, then the function will run again because the flag (hasCodeRunBefore
) will have been removed.
Good news...
Using localStorage
can be tedious because of operators and long winded function names. I created a basic module to simplify this, so the above code would be replaced with:
window.onload = function () {
if (!ls.exists('has_code_run_before')) {
/** Your code here... **/
ls.set.single('has_code_run_before', true);
/** or... you can use a callback. **/
ls.set.single('has_code_run_before', true, function () {
/** Your code here... **/
});
}
};
Update #1
Per @PatrickRoberts comment, you can use the in
operator to see if a variable key exists in localStorage, so
if (localStorage.getItem('hasCodeRunBefore') === null)
becomes
if (!('hasCodeRunBefore' in localStorage))
and is less verbose and a lot cleaner.
Secondly, you can set values as you would an object (localStorage.hasCodeRunBefore = true
) though it will be stored as a string, not as boolean (or whatever data type your value is).
How to run a function when the page is loaded?
window.onload = codeAddress;
should work - here's a demo, and the full code:
<!DOCTYPE html><html> <head> <title>Test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> function codeAddress() { alert('ok'); } window.onload = codeAddress; </script> </head> <body> </body></html>
How to make JavaScript execute after page load?
These solutions will work:
As mentioned in comments use defer:
<script src="deferMe.js" defer></script>
or
<body onload="script();">
or
document.onload = function ...
or even
window.onload = function ...
Note that the last option is a better way to go since it is unobstrusive and is considered more standard.
Related Topics
Why Doesn't a JavaScript Return Statement Work When the Return Value Is on a New Line
Format Numbers in JavaScript Similar to C#
Execute Script After Specific Delay Using JavaScript
Logical Operators in JavaScript - How to Use Them
How to Write a Named Arrow Function in Es2015
How to Get Progress from Xmlhttprequest
How to Escape Regular Expression Special Characters Using JavaScript
How to Make Js Know About the Application Root
How to Read the Client's MAChine/Computer Name from the Browser
JavaScript Setinterval and 'This' Solution
Es6 Destructuring Function Parameter - Naming Root Object
What Is the Stability of the Array.Sort() Method in Different Browsers
After Calling Chrome.Tabs.Query, the Results Are Not Available
Is ".Then(Function(A){ Return A; })" a No-Op for Promises
Is There a More Accurate Way to Create a JavaScript Timer Than Settimeout
How to Hide JavaScript Code in a Webpage
How to Listen to a "Style Change" Event
Jquery Xml Error ' No 'Access-Control-Allow-Origin' Header Is Present on the Requested Resource.'