3raser Posted April 5, 2011 Share Posted April 5, 2011 Here is my code: <html> <head> <title>Hey there, punk!</title> </head> <body> <script type="text/javascript"> function write() { alert("hey"); } </script> <form> <input type="button" value="TouchMe" onclick="write()"> </form> </html> </html> Whenever the button is clicked, the alert box doesn't show up. Quote Link to comment https://forums.phpfreaks.com/topic/232721-doesnt-run-the-function/ Share on other sites More sharing options...
Zane Posted April 5, 2011 Share Posted April 5, 2011 it's onClick.. not onclick EDIT: well, I thought it was case-sensitive. Try setting a proper DOCTYPE Quote Link to comment https://forums.phpfreaks.com/topic/232721-doesnt-run-the-function/#findComment-1197028 Share on other sites More sharing options...
requinix Posted April 5, 2011 Share Posted April 5, 2011 In that onclick, "write" refers to document.write, not window.write (which is what your function actually is). Try both: onclick="window.write()" document.write = function() { alert("document.write"); }; ...onclick="write()"... Quote Link to comment https://forums.phpfreaks.com/topic/232721-doesnt-run-the-function/#findComment-1197030 Share on other sites More sharing options...
PFMaBiSmAd Posted April 5, 2011 Share Posted April 5, 2011 If you rename your function to something a little less like a built in function name, your code will work. Quote Link to comment https://forums.phpfreaks.com/topic/232721-doesnt-run-the-function/#findComment-1197035 Share on other sites More sharing options...
Zane Posted April 5, 2011 Share Posted April 5, 2011 write isn't exactly a reserved word in Javascript http://www.webdevelopersnotes.com/tutorials/javascript/reserved.php3 Though it should be.. maybe that's an old post or something. Only way to find out is to use a different name for your function. Quote Link to comment https://forums.phpfreaks.com/topic/232721-doesnt-run-the-function/#findComment-1197038 Share on other sites More sharing options...
Adam Posted April 5, 2011 Share Posted April 5, 2011 it's onClick.. not onclick EDIT: well, I thought it was case-sensitive. Try setting a proper DOCTYPE Only XHTML is case-sensitive, but in that case it would be written 'onclick' anyway. Edit: Also it depends on the context in which the event is assigned; from within a window.onload event for example, it would refer to window.wirte(). If defined within the document, for example during the onclick HTML event, then it would refer to document.write(). So this would work (not 100% on browser compatibility): <input type="button" value="TouchMe" onclick="write('hey');"> But it's not exactly great JavaScript. As requinix said though, changing it to window.write() will work. Quote Link to comment https://forums.phpfreaks.com/topic/232721-doesnt-run-the-function/#findComment-1197075 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.