admin 管理员组文章数量: 1086019
I have the code that I have posted below in my webpage. As you can see it uses the body onload function to launch the script. Is there any way of making this script work without this and in JavaScript instead?
<!doctype html>
<html>
<head>
<script type="text/javascript">
function box(query){
return document.getElementById(query)
}
</script>
</head>
<body onload="box('query').focus();">
<input id="query" type="text">
</body>
</html>
Thanks in advance, Callum
I have the code that I have posted below in my webpage. As you can see it uses the body onload function to launch the script. Is there any way of making this script work without this and in JavaScript instead?
<!doctype html>
<html>
<head>
<script type="text/javascript">
function box(query){
return document.getElementById(query)
}
</script>
</head>
<body onload="box('query').focus();">
<input id="query" type="text">
</body>
</html>
Thanks in advance, Callum
Share Improve this question edited Jan 3, 2012 at 20:18 Josh Darnell 11.4k9 gold badges39 silver badges66 bronze badges asked May 1, 2011 at 21:06 Callum WhyteCallum Whyte 2,42911 gold badges38 silver badges56 bronze badges 4- Since the onLoad function is provided by JavaScript, you have already solved your problem. – Zian Choy Commented May 1, 2011 at 21:08
- what are you trying to achieve actually – Anupam Commented May 1, 2011 at 21:09
- 1 You're already using JavaScript. And how would you do something onload if not on the onload event? Your question makes no sense in it's current form. – no.good.at.coding Commented May 1, 2011 at 21:14
- i just cant understand y do u want to do that but if u dislike the onload function you can use document.ready function of jquery but again,it makes no sense to avoid onload in the first place – Anupam Commented May 1, 2011 at 21:20
4 Answers
Reset to default 1This might what you're looking for: Attach a body onload event with JS
I'd suggest using jQuery or some other JavaScript framework unless you're exploring JS or have some stringent restrictions on using libraries and frameworks.
the following code helpful for you
this is jquery:
$(document).ready(function(){
function name();
});
or
$(window).load(function(){
function name();
});
the following answers using javascript:
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function load() {
alert('function loaded');
}
window.onload = load;
</script>
</head>
<body>
</body>
</html
----------
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function load() {
alert('function loaded');
}
</script>
</head>
<body onload="load();">
</body>
</html
if you are talking about catching the onload event with writting any javascript in your html you can use this function:
// Add event
var addEvent = function (obj, evType, fn){
if (obj.addEventListener){
obj.addEventListener(evType, fn, false);
return true;
}else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
return false;
}
};
Now you can run all the functions you need in your onload event.
addEvent(window,'load',function () {
box('query').focus();
// or
document.getElementById('query').focus();
});
本文标签: htmlBody onload function in JavaScriptStack Overflow
版权声明:本文标题:html - Body onload function in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744028667a2521097.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论