(1) The easiest way to call is to write it directly into the body tag of html:
<body onload="myFunction()"></body>
<script type="text/javascript">
function myFunction(){
alert(" Hello, let's get started!");
}
</script>
<body onload="myFunction1();myFunction2('123');"></body>
<script type="text/javascript">
function myFunction1( ){
alert("1, hello, let's start!");
}
function myFunction2(obj){
alert("2, hello, let's start!" + obj);
}
</script>
(2) Call in JS statement (1):
<script type="text/javascript">
function myFunction(){
alert("Hello, let's start!");
}
window.onload = myFunction;// without parentheses
window.onload = myFunction();// parentheses can pass parameters
</script>
( 3) Call in JS statement (2):
window.onload = function (){// Anonymous call
alert("Hello, let's start!");
};
window.onload = function myFunction(){
alert("You Okay, let's get started!");
};
There are three commonly used methods for automatic execution of JS functions in web pages
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324967956&siteId=291194637
Ranking