Pages

How to add javascript to web pages ?

There are three ways to add javascript to web pages i.e.

1)    Writing javascript code along HTML e.g.  <a href="xyz.html" onclick="JavaScript code">  where  onclick javascript event defines what action will be taken when user clicks on the xyz.html hyperlink .


2)   Adding javascript at the top of the html file  as :
<script type="text/javascript"><!--//--><![CDATA[//><!--JavaScript code //--><!]]></script> .
Don't worry if you don't understand it all .<!--//--><![CDATA[//><!-- //--><!]]> This part is for browsers to understand . To be put in simple language you can also do with  :
              <script type="text/javascript">JavaScript code</script>


3) Put your javascript code in a separate file and access it from your HTML file e.g.
             
                      <script type="text/javascript" src="abc.js"></script>
abc.js is the file that is accessed on the fly . .js extension is associated with the javascript files .
One of the advantages of using javascript in a different file is that you can apply same javascript  rules to different pages by calling the .js file from every page . src attribute is used to provide the location of the javascript file .