harshpandya Posted March 7, 2008 Share Posted March 7, 2008 HiThis code should get the button and when clicked call function f();parent.document.getElementById("start").onclick = f();but it seems it just call the function as soon as page loads. Why is it not checking weather button is clicked?Help Link to comment Share on other sites More sharing options...
harshpandya Posted March 7, 2008 Author Share Posted March 7, 2008 Here I found it window.parent.document.getElementById("Id").onclick = f;i had typo, but this works fine. Thanks Link to comment Share on other sites More sharing options...
jeffman Posted March 7, 2008 Share Posted March 7, 2008 EDIT: Ok, you figured it out. But what follows is a really great answer!The script thinks you are asking it to assign the return value of f() to parent.document.getElementById("start").onclick, so it runs the function right away to get the value for you. To assign the function itself (yeah, that sounds sloppy) to .onclick, you have 3 choices, each one just slightly different from what you tried.parent.document.getElementById("start").onclick = "f()";orparent.document.getElementById("start").onclick = f;or the somewhat fancier:parent.document.getElementById("start").onclick = function (){f();} ;The last is useful if you want to pass multiple parameters and execute multiple statements but for some reason don't want to create a public function. That might look like this:parent.document.getElementById("start").onclick = function (this, event){f(this.value); var a = check(event||window.event); if (a) {alert(this.message);}} ;Most folks use the first version. Real programmer types use the second. Idiots like me use the third. Link to comment Share on other sites More sharing options...
Synook Posted March 7, 2008 Share Posted March 7, 2008 There is also addeventlistener() if you want to be really advanced :)Have a look at http://www.quirksmode.org/js/events_advanced.html Link to comment Share on other sites More sharing options...
aspnetguy Posted March 7, 2008 Share Posted March 7, 2008 There is also addeventlistener() if you want to be really advanced Have a look at http://www.quirksmode.org/js/events_advanced.html in browsers other than IE. In IE you have to use attachEvent() Link to comment Share on other sites More sharing options...
harshpandya Posted March 8, 2008 Author Share Posted March 8, 2008 ADDEVENTLISTENER IS GOOD STUFF ALSOTHANKS Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.