最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling a function when page loads - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 7

Avoid 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.

发布评论

评论列表(0)

  1. 暂无评论