Popup from within an <area>-tag

Viewed 293

First things first: I am an absolute novice to javascript but have the feeling I have to learn it. Here's my problem:

  1. I have a custom map made from a photo with defined map areas.

  2. I want to show a popup window with additional information AND a picture (like Google Maps). My guess is that I need javascript to do this. So far, I searched the web up and down but only found snippets that let the user hit a button to show the popup. Here's the html-code I have so far:

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../css/basic.css">
<link rel="stylesheet" href="../css/karten.css">
<link href="//cdn.jsdelivr.net/npm/featherlight@1.7.14/release/featherlight.min.css" type="text/css" rel="stylesheet" />
<link href="//cdn.jsdelivr.net/npm/featherlight@1.7.14/release/featherlight.gallery.min.css" type="text/css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-latest.js"></script>
<script src="//cdn.jsdelivr.net/npm/featherlight@1.7.14/release/featherlight.min.js" type="text/javascript" charset="utf-8"></script>
<script src="//cdn.jsdelivr.net/npm/featherlight@1.7.14/release/featherlight.gallery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/detect_swipe/2.1.1/jquery.detect_swipe.min.js"></script>

<title>West Pasco Model Railroad Association</title>
<div class="container">

    <div class="header">

        <a href="index.html"><img class="img-full" src="../graphics/header-pic.jpg" alt="WPMRA Header Image" ></a>

    </div>
    <div id="map">
        <img src="refinery-fells-point-96dp-clean.gif" usemap="#fellspoint">
        <map name="fellspoint">
            <area shape="poly" coords="209,352,483,367,522,357,581,347,619,330,641,324,633,279,344,283" 
               id="refinery" alt="Refinery" href="#refinery" title="West Side Refinery, 2 Tracks, 10 cars"
            />
            <area shape="poly" coords="892,209,938,212,990,210,1028,185,1043,170,1041,143,1026,127,945,161,900,191,897,203" 
                alt="Scrapyard" target="scrap-yard" href="#scrapyard" 
            />
            <area shape="poly" coords="1091,267,1185,273,1188,242,1177,201,1088,201,1085,244" 
                alt="Brewery" target="eagles-nest-brewery" href="#eaglesnest-brewery" title="Eagles Nest Brewery, 2 tracks, 4 cars"
            />
            <area shape="poly" coords="1075,319,1118,386,1179,354,1134,282" 
                alt="Ralphs Classic Autoparts" target="classicautoparts"  href="#classicautoparts" 
            />
            <area shape="poly" coords="1020,371,1090,342,1056,284,988,312" 
                alt="The News Post and Sunday Gazette" target="news-post"  href="#news-post" 
            />
            <area shape="poly" coords="938,361,995,329,968,286,920,323" 
                alt="Reliable Warehouse and Storage" target="reliablewarehouse" href="#reliable-warehouse-and-storage" 
            />
            <area shape="poly" coords="873,358,919,317,907,285,852,329" 
                alt="Swift" target="swift" href="#swift" 
            />
            <area shape="rect" coords="1005,385,1079,439" 
                alt="Muir Beaver Express" target="beaverexpress" href="#beaverexpress" 
            />
            <area shape="rect" coords="813,377,983,430" 
                alt="IGA" target="iga" href="#iga" 
            />
            <area shape="poly" coords="1073,167,1163,143,1150,119,1059,87,967,113,994,132,1064,119" 
                alt="West Side Fuel and Oil" target="westsidefuelandoil" href="#westsidefuelandoil" 
            />
            <area shape="poly" coords="808,348,868,309,846,265,770,313" 
                alt="Imperial Foods" target="imperialfoods" href="#imperialfoods" 
            />

        </map>
       
    </div>
    
</div>

I assume that I have to reference the "href" attribute somehow but have to clue how to do this. Did anybody do this already and can help me out, please?

1 Answers

To get you started, here's a way to respond to a click event over an area element in the image map:

function popupModal(event) {
   event.preventDefault();  // assuming no link action to be taken
   const area = event.target;
   const windowName = area.target;
   const href = area.getAttribute("href");

   // create (modal?) pop up

   // insert code to use area, href and/or windowName values here
   console.log('windowName: "%s", href: "%s"', windowName, href, area);
   //...
}

// add click event listener

document.querySelector("map").addEventListener("click", popupModal);

Note the code to add the event listener must be placed (or included from file with script tags) at the foot of the body section, after HTML to create the map element has been parsed by the browser, or called in an event listener for DOMContentLoaded or window load events. Otherwise it won't find the MAP element.

The href attribute value obtained from the area element only contains the text specified in HTML source - so you might get, say, "#imperialfoods" and not the complete http://example.com/folder/page.html#imperialfoods link reference which may be more than needed.

I don't know if you intend using featherlight or jQuery in your code - the above example is vanilla JavaScript.

If you have further questions about creating a modal popup please ask a new question and include the code you've written that attempts to create one - multiple questions on multiple topics in one post may prevent future readers finding solutions for only one of them.

Related