Why isn't JavaScript running when I refernce the external file in between the head tags in my html file? -



Why isn't JavaScript running when I refernce the external file in between the head tags in my html file? -

when set link js file in between html header tags js file not run. if set external js link after body tag or somewhere after mass of html code file runs. there way create external js file run when link external file in between head tags. should mention i'm doing html dom in external js file.

html portion:

<!doctype html> <html> <head> <title>htmldom practice</title> <link rel="stylesheet" type="text/css" href="htmldom practice.css"/> <!--i set external js file here--> </head> <body> <div id="box"> <p id="txt">hello world!</p> </div> </body> <script src="htmldom practice.js"></script> </html>

javascript/html dom portion:

var x = document.getelementbyid('txt'); var b = document.getelementbyid('box'); x.addeventlistener('click',irt,false); b.addeventlistener('click',irt,false); function irt(){ x.innerhtml="goodbye world!"; b.style.padding="20px"; }

i know can annoying place js code @ bottom of page utilize dom. practise load js in head.

you workaround this. 1) place function in js file initialising 2) phone call initialise function onload.

js file looks this

var x; var b; function irt(){ x.innerhtml="goodbye world!"; b.style.padding="20px"; } function seteverything(){ x = document.getelementbyid('txt'); b = document.getelementbyid('box'); x.addeventlistener('click',irt,false); b.addeventlistener('click',irt,false); }

html body tag

<body onload="seteverything()">

javascript html dom

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -