Converting inline Javascript to external -


i have following inline javascript (to show version on mouseover) have working. i've been told needs external, created .js file , linked in head, removed tags can't working linked js file. i'm new this, , i'm sure simple i'm not understanding, appreciated.

<!doctype html>  <html> <head>   <title>template_test</title>   <link href="styles.css" rel="stylesheet" type="text/css" media="screen"> </head>  <body>   <div class="section group box">     <img src="icon-student.png" width="75" height="75">      <h1>sub title section</h1>      <p>ut molestie mattis ultrices. suspendisse malesuada turpis non neque tincidunt     dignissim. curabitur venenatis vehicula augue, ac venenatis felis aliquam ut.     curabitur aliquet turpis nec lorem commodo.</p>   </div>    <div id="versionhidden" onmouseover=   "document.getelementbyid('hiddenversion').style.display = 'block';" onmouseout=   "document.getelementbyid('hiddenversion').style.display = 'none';">     <div id="hiddenversion" style="display: none;">       version 2.0     </div>   </div>    <div id="clear">     <br>   </div> </body> </html> 

added <head> of html:

<script src="myjsfile.js"></script> 

in external javascript file (per example, "myjsfile.js"):

window.addeventlistener('domcontentloaded', function () {     document.getelementbyid('versionhidden').addeventlistener('mouseover', function () {        document.getelementbyid('hiddenversion').style.display= 'block';     }, false);      document.getelementbyid('versionhidden').addeventlistener('mouseout', function () {       document.getelementbyid('hiddenversion').style.display = 'none';     }, false); }, false); 

the advantage of doing don't need inline , can therefore have full control of behaviors in web app without needing mess document structure.

and add code wait domcontentloaded event when put our script tag in head (as opposed bottom of page) because there no "versionhidden" element @ time browser renders script tag if script tag before "versionhidden" element, document.getelementbyid('versionhidden') null if didn't wait domcontentloaded event.

putting script tag @ end of document avoids need domcontentloaded, , recommended yahoo performance (as browser doesn't block loading , rendering rest of page content while script running), 1) may not want page visible until script loaded, , 2) purists (like myself) feel scripts not part of document, should not, conceptual opposed practical reasons, put in body in head.

note code above requires modern browser work; support older browsers, consider using jquery.

note above code won't useful since using display none mean element removed entirely, there nothing mouseover!

you want use instead:

window.addeventlistener('domcontentloaded', function () {     document.getelementbyid('versionhidden').addeventlistener('mouseover', function () {        document.getelementbyid('hiddenversion').style.visibility = 'visible';     }, false);      document.getelementbyid('versionhidden').addeventlistener('mouseout', function () {       document.getelementbyid('hiddenversion').style.visibility = 'hidden';     }, false); }, false); 

...and change html element to:

<div id="hiddenversion" style="visibility: hidden"> 

this reserves space item, makes hidden.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -