Executing javascript function with image map in HTML - javascript

I want to execute some a javascript function when i click on a certain part of the image in my cshtml page. However the clickable area does not appear.
I also dont want to reference www.google.com but it requires me to have alt and href attributes.
Why is my code not working?
Here is my code:
<image src="~/Content/Images/PeriodicTable2.jpg" width="900" height="450" alt="Sample Image" />
<map name="PeriodicTable">
<area shape="rect" coords="0,0,200,200" onclick="Popup('Hydrogen')" href="https://www.google.com" alt="something">
</map>

You have to set the usemap attribute:
<img src="..." usemap="#PeriodicTable">
See the following example, where I added a dummy Popup() function to test:
function Popup(text) {
console.log(text)
}
<img src="https://via.placeholder.com/600x200" usemap="#PeriodicTable" width="600" height="200" alt="Sample Image">
<map name="PeriodicTable">
<area shape="rect" coords="0,0,200,200" onclick="Popup('Hydrogen')" href="#" alt="something">
</map>
As for the <area> and its attributes, HTML4 requires both the alt and href to be set (alternatively the nohref attribute).
In HTML5, href is optional and alt is required only if href is used.
As an <area> without a href is not considered a regular link, browsers will not show the mouse changing to a pointer when you hover the areas. This behavior can be changed using CSS: area { cursor: pointer }.
This means the following example should work pretty much like the first one, but without the href and alt attributes.
function Popup(text) {
console.log(text)
}
area {
cursor: pointer;
}
<img src="https://via.placeholder.com/600x200" usemap="#PeriodicTable" width="600" height="200" alt="Sample Image">
<map name="PeriodicTable">
<area shape="rect" coords="0,0,200,200" onclick="Popup('Hydrogen')">
</map>

onclick="Popup('Hydrogen')" it is a function, I think you must put semi-colon ';' after the code. Like this onclick="Popup('Hydrogen');"

Related

Replace this.href with this.title

The below code works perfectly to reveal an image when a user mouses over an hotspot of an image map. The only problem? When clicked 'this.href' of course takes the user to the hotspot image itself. I'd like to reserve 'href' to send the users to the page of my choosing.
Is it possible to use "title" rather than 'href' to define the hotspot image? If so, please show details, I'm very new to javascript, thank you!
<script type="text/javascript">
function ShowPicC(sImage){document.housec.src = sImage}
</script>
<img border="0" src="main-image.png" width="640" height='640' usemap="#FPMap0">
<map name="FPMap0">
<area onmouseover='ShowPicC(this.href)' href='hotspot1.png' alt="thisalt" title="can-png-go-here.png" shape="circle" coords="400,400,20">
<area onmouseover="ShowPicC(this.href)" href="hotspot2.jpg" alt="youralt" title="can-png-go-here2.png" shape="circle" coords="420,420,20">
<area onmouseover="ShowPicC(this.href)" href="hotspot3.jpg" alt="differentalt" title="can-png-go-here3.png" shape="circle" coords="440,440,20">
<area onmouseover="ShowPicC(this.href)" href="hotspot4.jpg" alt="whateveralt" title="can-png-go-here4.png" shape="circle" coords="460,460,20">
</map>
<img name="housec" src="starter-hotspot-image.png" width="192" height="170">
Sure, although title isn't a valid property of area. Did you try it?
<area onmouseover="ShowPicC(this.title)" title="someotherimage.jpg" ...
You may be better off using a data attribute:
<area data-image="someotherimage.jpg" ...
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes

Set hyperlinks to certain sections of the same picture? (HTML)

I want to have certain parts of the same picture hyperlinked to different webpages
Is there a way I can do this with javascript coordinates or any other way?
You can do it with the HTML MAP tag, for example:
<img src="http://www.w3.org/html/logo/downloads/HTML5_Logo_512.png"
width="512" height="512" alt="Logo" usemap="#logomap"/>
<map name="logomap">
<area shape="rect" coords="0,0,256,512"
href="javascript:alert('left')" alt="Left">
<area shape="rect" coords="256,0,512,512"
href="javascript:alert('right')" alt="Right">
</map>
Here's fiddle to try it out. Clicking on the left side of the image will show a Javascript alert that says left, on the right side it will show right.

Jump to a specific location on an image map

I have a big image with an HTML <map>, and I want to jump to a particular region on that image. I have used the <area /> tag for marking the locations
Take a look at the code :
<img src="demo_files/k3.png" id="target" alt="Map" usemap="#powerpuffgirls" />
<map name="powerpuffgirls">
<area shape="rect" coords="624,137,671,167" href="#" id="ppg" title="The Powerpuff Girls" alt="The Powerpuff Girls" />
<area shape="rect" coords="99,2685,161,2723" href="#" name="ppg1" title="The Powerpuff Gidrls" alt="The Powerpuff sGirls" />
</map>
however, I am unable to move to any region on the image.
Edit: Any other approach for moving to an image's particular region would be great !!
Try this link
$('a.links').click(function(e){
e.preventDefault();
var coor = $(this.hash).attr('coords').split(',');
$('html,body').scrollTo(coor[0], coor[1]);
});
i have used the plugin scrollTo
the script will prevent default function of a tag and will get the coordinates attribute from the area tag with the id from the href attribute and calculate the positions and scroll to that position
Check this demo i have created...
Try to navigate the areas by id...
<div>Go to one Go to two</div>
You have to play with the area coordinates in it...
http://jsfiddle.net/D9W6C/

event.target/srcElement not working 100% in Chrome and Opera

I'm trying to get the target/srcElement of an HTML "area" element for an image map.
Here's what I have so far (pardon the data URI):
<map name="dpad">
<area id="dpad"shape="rect" alt="" title="" coords="1,14,12,24" onmousedown="move('l',false,event);" target="" />
<area shape="rect" alt="" title="" coords="15,2,24,12" onmousedown="move('u');" target="" />
<area shape="rect" alt="" title="" coords="27,15,38,24" onmousedown="move('r');" target="" />
<area shape="rect" alt="" title="" coords="15,27,25,38" onmousedown="move('d');" target=""/>
</map>
<img width="40"height="40"src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAABp0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuMTAw9HKhAAADDElEQVRYR+2ZvYoiQRSFW1EMVkEzI2XfydQH2GwGDTfczFRYNNBBUDFQUFN9ATXQwEEDRQ3UB3ADQajpr7Gkp8f+k2G6F2bg0jVVt+qcrp9zb5cBxeVfMpmMnE6nX+fz+U8kEvkRCoUsR7hcLorq+0/1/R2NRv8ej8ezS0h37uFwOBEMBkupVErk83lRKBQsDR986UNfd2gPeAcCgYRqpUwmI+bzuTgcDjcbj8cC09fhgy996PsApLsukmA2mxWLxeJGhnKz2dTMWI+vpwRXq5XodDqi3W5rRpk6ZhKynhLc7XZiMBiIVqsler2eZpSpo81zgqPRSDQaDdHtdkW/39eMMnXsR08JTqdTUSqVRK1WE/V6/Z1RVy6XBT6eLTEzNJlMLA0fzwjqT6teWvRlT5f4myASnU6nI4SgqwA7ef5UfV+MQu1giV/UfvR1gpGAE9wUNYA/EyevSu/kCdDrAwRfeTGnOHCKxWLPipqNnAjmxEtAnVqxWBTr9fpd3L03i/jg63Rc/OACJ7gxg1pWQlBn0+sNDZNia2wDeL/f3wgSNWazmWaUJVl88DX253/GBsPYBhc4wU2Jx+NauqR/ewCIEIgwOme2v6jfbDZiOByKarWqCTZWqVS0UEebVV/GBgMs/UvRB05w+0CQIM/ghCrA7AhCjihCDJahjjJ1tNkRBAMsMGWCYUqQqSYTIdgTTwGxIshbM3N6cnqStG23W1OSjA0GWGCCLbX1wwyyH8jlSJckoB1B9hszIEkZn7ThYzaLkiD9wAQbDnD5/wjylsvl8lOXmMPidonh8GWHhI3/qYdEDsbmZx+Qz9md4nsyw+HgBDuRGTDAMpUZxDCXy5kKNQTviaxRqFlKKdT6ZbUSasY2E2o4aULt+1BHQPZ1suD7dMvdZ7uimH24O8gHvb1Z+CbodKn1Sywvi+yenn12omEIrUyZSJuk+eLDXWbFvr36gKDvL484vb6+fpPy4rsLTN9eAesv0ck47C7R8fnSS3R+hlDToCeV6Il0iE9DK8Pn6vtEX6d6K/3eAH0oKaUOiMCyAAAAAElFTkSuQmCC"usemap="#dpad"/>
So, for the left arrow on the dpad, I have a function to be called onmousedown, move(dir,hold,e).
Here is the function started:
function move(dir,hold,e){
var p=document.getElementById('test');
alert(e);
}
This is confusing the crap out of me as to why I'm getting "undefined" when clicking on the area. Could someone please explain in FULL detail as to how I can get the target?
Do I need to include 'event' as a parameter in my HTML code? Do I even NEED a parameter for it?
On another note, here's something that really confused me.
So, I can't get the target of that element, but this works just fine:
<div class="male1-d" id="test" data-sprite="male1" onclick="alert(event.target+' '+this.style.left+' '+this.style.top);"></div>
Done. Target obtained. Now why can't I do it in a function?
I forgot to mention that for Opera I meant mobile(?) version 9.50.
in some browsers,'event' is a attribute of window.But you need to include 'event' as a parameter in others.So your move function should be like below:
<html>
<body>
<button onmousedown='move(event)' id='btn' name='a button'>click</button>
</body>
<script>
function move(e){
var e=e||window.event;
var target=e.target||e.srcElement;
alert(target);
alert(target.id);
alert(target.name);
}
</script>
</html>
It seems , it is a problem in the click event.
They says , it is a bug in jquery. And has given details in
Jquery click event of a div in li element not fired in mobile Safari
It says two things:
Add cursor:pointer to the DIV class.
Add onclick="" to the element

How do you JavaScript style area elements on mouseover?

I've got an image map with 20 area elements, only four shown below. I want to style each area so that a blue border appears whenever a user hovers over it - all the area shapes are rectangles.
<map id="mymap" name="mymap">
<area shape="rect" coords="0,0,223,221" href="http://..." />
<area shape="rect" coords="226,0,448,221" href="http://..." />
<area shape="rect" coords="451,0,673,223" href="http://..." />
<area shape="rect" coords="677,0,1122,223" href="http://..." />
...
</map>
I've tried using CSS to style each area, but it's not working. And I've tried to put an onmouseover=color() on the map element and call the following function, but that doesn't seem to be working either:
function color() {
var blueboxes = document.getElementsByTagName('area');
for(var i=0; i<blueboxes.length; i++) {
blueboxes[i].style.border = 'solid blue 5px';
}
}
mapper.js can be used for this.
Mapper.js 2.4 allows you to add automatic area highlighting to image maps on your webpages (inc. export to SVG).
It works in all the major browsers - Mozilla Firefox 1.5+, Opera 9+, Safari and IE6+. On older browsers, it can use "jsgraphics" from Walter Zorn (if installed), else it'll degrade and your visitors won't notice a thing.
Sample code from that website:
Please note that everything below this line is his code and wording, not mine. Full attribution belongs to the link above.
Setting Up
Download mapper.js and include it into your webpage.
<script type="text/javascript" src="wz_jsgraphics.js"></script>
<script type="text/javascript" src="mapper.js"></script>
"wz_jsgraphics.js" is copyright by Walter Zorn and not part of the distribution!
Using It
To get the highlighting just add a class="mapper" to an div surrounded image.
<div>
<img src="..." class="mapper" usemap="..." alt="...">
</div>
To get individual area highlightings add one or more classes to the area.
<map>
...
<area shape="poly" class="noborder icolor00ff00" href="#" coords="...">
...
</map>
To get multiple area selections add one or more id's to the areas rel attribute.
<map>
...
<area shape="poly" id="blue" rel="green,red" href="#" coords="...">
<area shape="poly" id="green" rel="red,blue" href="#" coords="...">
<area shape="poly" id="red" rel="green,blue" href="#" coords="...">
...
</map>
To force a group of areas using the attributes of the initial area.
<map>
...
<area shape="rect" id="black" class="icolor000000 forcegroup" rel="green,red,blue" href="#" coords="...">
...
</map>
The area tag can't be styled like a normal anchor. I would use a different approach. You could apply your image to a div as the background-image and then position clickable elements over the div by using position: absolute.
Take a look at this technique: http://www.alistapart.com/articles/cssmaps/
Can Raphaeljs helps?
Have a look at this sample :)
Usually the approach I see is to build the imagemap itself out of different images in CSS. Here's a good example of this:
http://ago.tanfa.co.uk/css/examples/europe-map.html

Categories