admin管理员组文章数量:1395237
My code
This is the code which I have written. The default
function should be called when the page loads, without clicking any button. It should display some data and then two buttons should be displayed. What modifications do I need in that code?
I am a beginner so I may have missed many things please help.
This is my JavaScript part, detailed program is given in the link.
function default(){
var i=7;
j=15;
document.write(i+j);
}
My code
This is the code which I have written. The default
function should be called when the page loads, without clicking any button. It should display some data and then two buttons should be displayed. What modifications do I need in that code?
I am a beginner so I may have missed many things please help.
This is my JavaScript part, detailed program is given in the link.
function default(){
var i=7;
j=15;
document.write(i+j);
}
Share
Improve this question
edited Apr 11, 2013 at 16:49
NewUser
asked May 20, 2012 at 17:41
NewUserNewUser
3,82911 gold badges60 silver badges84 bronze badges
1
-
When or under which circumstances should your
default
function be called? When the page has loaded? After a certain amount of time? Or when some other event occurs? – stakx - no longer contributing Commented May 20, 2012 at 17:44
4 Answers
Reset to default 7Avoid using Keywords
You cannot use the keyword default
:
Uncaught SyntaxError: Unexpected token default
You'll need to rename your function.
Immediately Invoked Function Expressions
Once you've done that, you can wrap it in parenthesis to execute it immediately:
(function dflt(){
var i = 7, j = 15;
document.write( i + j );
})();
Warning: Avoid document.write
Note that document.write
will clear out your document, thus removing the contents of your document. You should instead insert a new text node, or update the contents of an element on the page:
(function dflt(){
var i = 7, j = 15;
document.body.appendChild( document.createTextNode( i + j ) );
})();
You could also add this text node to an element on the page:
var tNode = document.createTextNode( i + j );
document.getElementById("foo").appendChild( tNode );
This would add the resulting text to any element whose id
attribute is foo
:
<span id="foo"></span>
You can use Immediately Invoked Function Expression
:
(function defaults() { // I edited the function name - "default" can not be used
var i = 7; // You can also remove the function name, will work either
j = 15; // way. "default" is a reserved word
document.write(i + j);
})();
But it's calling it... you can't run a function without calling it, do you want to execute it with magic?
You should read this:
What does this "(function(){});", a function inside brackets, mean in javascript?
As @stakx pointed out, you can pass a function reference to functions the execute the function after X time, like setTimeout
:
setTimeout(defaults, 1000); // execute "defaults" after one second.
But it's just like handle it yourself:
function foo(aFunction){
aFunction();
}
foo(defaults);
If you're not going to use jQuery (as suggested by @Photon Critical Fatal Error), you could give the body
element's onload
event a try:
<body onload="default();">
...
</body>
Though this might not work as expected. jQuery handles this in a more reliable way, so if you decide to give it a try, look at the $(document).ready(…)
API function.
function default(){
var i=7;
j=15;
document.write(i+j);
}
default is a reserved word in javascript you cant use it.
本文标签: javascriptCalling a function when page loadsStack Overflow
版权声明:本文标题:javascript - Calling a function when page loads - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744704505a2620758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论