Computer >> Computer tutorials >  >> Programming >> Javascript

How to use JavaScript to create client-side image map?


Use JavaScript to create client-side image map. Client-side image maps are enabled by the usemap attribute for the <img /> tag and defined by special <map> and <area> extension tags.

The image that is going to form the map is inserted into the page using the <img /> element as normal, except that it carries an extra attribute called usemap. The value of the usemap attribute is the value of the name attribute on the <map> element, which you are about to meet, preceded by a pound or hash sign.

Example

You can try to run the following code to create a client-side image map −

<html>
   <head>
      <title>Using JavaScript Image Map</title>
      <script type="text/javascript">
         <!--
            function showTutorial(name) {
               document.myform.stage.value = name
            }
         //-->
      </script>
   </head>
   <body>
      <form name="myform">
         <input type = "text" name = "stage" size = "20" />
      </form>

      <!-- Create Mappings -->
      <img src="/images/usemap.gif" alt="HTML Map" border="0" usemap="#tutorials"/>
         
      <map name="tutorials">
         <area shape="poly"
            coords="74,0,113,29,98,72,52,72,38,27"
            href="/perl/index.htm" alt="Perl Tutorial"
            target="_self"
            onMouseOver="showTutorial('perl')"
            onMouseOut="showTutorial('')"/>

         <area shape="rect"
            coords="22,83,126,125"
            href="/html/index.htm" alt="HTML Tutorial"
            target="_self"
            onMouseOver="showTutorial('html')"
            onMouseOut="showTutorial('')"/>

         <area shape="circle"
            coords="73,168,32"
            href="/php/index.htm" alt="PHP Tutorial"
            target="_self"
            onMouseOver="showTutorial('php')"
            onMouseOut="showTutorial('')"/>
      </map>
   </body>
</html>