[英]Call <body onload=“function()”> in javascript file
I've called two functions in my HTML 我在HTML中调用了两个函数
<body onload="myFunction()" onmousemove="myFunction1()">
I want to execute these two functions from a javascript
file and call this file in my HTML
like 我想从一个javascript
文件执行这两个功能,并在我的HTML
调用该文件,例如
<script type="text/javascript" src="path/to/functions.js"></script>
ie both the functions will be called from the javascript file, not from html. 即这两个函数将从javascript文件而不是html调用。
I have tried 我努力了
window.addEventListener('load', myFunction()); window.addEventListener('onmousemove', myFunction1());
or 要么
$(window).load(function () {
myFunction();
});
$(window).onmousemove(function () {
myFunction1();
});
but failed. 但失败了。
What should I do ? 我该怎么办 ?
DETAILS 细节
I have a javascript file where some functions are there. 我有一个JavaScript文件,其中有一些功能。
script.js
function myFunction() {
alert("This is my function");
}
function myFunction1() {
alert("This is my second function");
}
I called these functions in my html body tag 我在html body标签中调用了这些函数
<body onload="myFunction()" onmousemove="myFunction1()">
Now I want to call these two functions from my script.js
like 现在我想从我的script.js
调用这两个函数
window.addEventListener('load', myFunction());
window.addEventListener('onmousemove', myFunction1());
what should I do ? 我该怎么办 ?
I think you have understood my requirement. 我认为您已经理解了我的要求。
Your code should read like this: 您的代码应如下所示:
window.addEventListener('load' , myFunction );
window.addEventListener('mousemove' , myFunction1 );
But if it still does not work for you, Try this: 但是,如果仍然无法使用,请尝试以下操作:
window.onload = myFunction ;
window.onmousemove = myFunction1 ;
Check this: onload and this: onmousemove 检查以下内容: onload和以下内容: onmousemove
Try 尝试
function number1(){ document.querySelector("#info").innerHTML = "LOAD"; } function number2(){ var x = event.clientX; var y = event.clientY; document.querySelector("#info").innerHTML = "MouseMove: ( x:"+x+" , y:"+y+") "; } window.onload = function(){ number1() } window.onmousemove = function(){ number2() }
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <div id="info">waiting</div> </body> </html>
I think You should verify that your (.js) file is loaded with the page or not? 我认为您应该验证页面是否已加载(.js)文件? Because i have tried both the example and both r working window.addEventListener('load', myFunction);
因为我已经尝试过示例和工作window.addEventListener('load', myFunction);
,所以都可以使用window.addEventListener('load', myFunction);
and window.addEventListener('load', myFunction());
和window.addEventListener('load', myFunction());
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("Page is loaded");
}
window.addEventListener('load', myFunction);
</script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
or 要么
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("Page is loaded");
}
window.addEventListener('load', myFunction());
</script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.