admin管理员组文章数量:1244270
I'm relatively new to JS, but I have programmed in C before, and I'm trying to get my head around the whole event driven nature of it. I'm trying to create a script that will validate form input, however all of my code is being run - everything inside if/else, loops, what have you - regardless of the event happening or not. To test, and to make it easier to post here, I've written a simple program that has this problem too.
HTML:
<button id="test">Test</button>
Javascript:
function init(){
document.getElementById("test").onclick = alert("hello");
}
window.onload = init;
From what I understand, the init function should be called when the page loads (window.onload), and the alert should pop up when the button with the ID "test" is clicked (onclick). What is actually happening is that the alert is popping up as soon as the page loads, and will pop up again if I hit the button.
My thinking is that I've made some incorrect assumptions about the order in which JS is executed, but I cant think of what that is. Is anyone able to shed some light on this?
I'm relatively new to JS, but I have programmed in C before, and I'm trying to get my head around the whole event driven nature of it. I'm trying to create a script that will validate form input, however all of my code is being run - everything inside if/else, loops, what have you - regardless of the event happening or not. To test, and to make it easier to post here, I've written a simple program that has this problem too.
HTML:
<button id="test">Test</button>
Javascript:
function init(){
document.getElementById("test").onclick = alert("hello");
}
window.onload = init;
From what I understand, the init function should be called when the page loads (window.onload), and the alert should pop up when the button with the ID "test" is clicked (onclick). What is actually happening is that the alert is popping up as soon as the page loads, and will pop up again if I hit the button.
My thinking is that I've made some incorrect assumptions about the order in which JS is executed, but I cant think of what that is. Is anyone able to shed some light on this?
Share Improve this question asked May 1, 2015 at 8:38 Daniel ForsythDaniel Forsyth 3432 gold badges4 silver badges18 bronze badges2 Answers
Reset to default 10That's not right, it should be:
function init(){
document.getElementById("test").onclick = function () { alert("hello"); };
}
This is because you need in JavaScript you need to assign the function itself to the click
event (alert is a function).
Take this for example:
function hello() {
alert("hello");
}
document.getElementById("test").onclick = hello;
Note that I didn't put the brackets ()
after hello
function? That's because I'm using a reference to the function itself. If I put the brackets, I'm actually evaluating that function at the point in time of the click assignment happening.
So doing:
document.getElementById("test").onclick = hello();
Will show the alert will show the alert
immediately after this line has been executed.
yes your javascript function will call only when page will load .If you also want to call that init() function on click Test button so please try like this :
JavaScript:
function init(){ alert("hello"); }
window.onload = init;
HTML:
<button id="test" onClick='init()'>Test</button>
本文标签: htmlJavascript Functions get called on page load instead of onclickonsubmitStack Overflow
版权声明:本文标题:html - Javascript: Functions get called on page load instead of onclickonsubmit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740125462a2228770.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论