Thursday, 15 September 2011

javascript - reference to function before its defined -



javascript - reference to function before its defined -

i have read if want create site loads faster should set javascript @ end of html file following

<html> <body> </body> <script> //my awesome javascript functions go here because lets things load faster //than if @ top </script> </html>

the problem when create buttons or utilize onchange events phone call these functions. how meant maintain js @ bottom of page , have js defined when reads js function need called?

for example

<html> <body> <input type="text" onchange="myfunction()"/> </body> <script> function myfunction(){} </script> </html>

i did code in pseudo code-ishly wouldn't focus on of syntax errors, more focus on how meant go this. when creating page, creates html properly, gives me console error saying "myfunction" not defined. if move script part above body works, recommended maintain lastly increment speed in page load.

just note not using jquery.

i thought duplicate (javascript @ bottom, function phone call in body?) doesn't seem reply problem.

------------------------update---------------------------- using event listeners

<html> <body> <input type="text" id="myawesometext"/> </body> <script> function myfunction(){} element = document.getelementbyid('myawesometext'); element.addeventlistener("onchange", myfunction, false); </script> </html>

javascript html

No comments:

Post a Comment