Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

A bit about what I am trying to do:

At window.onload this page will AJAX call another php page and get a base64 encoded image as a response. This works fine. Once the image is returned via AJAX it is set as the src of 'img1'. Also, work's fine. The image also, because of maphilight has an area map with coordinates that are also pulled and set with same AJAX call. It is here that the maphilight doesn't begin working on, of course, internet explorer. I've tested it on all other browsers and highlights work fine. My question is how do I get the highlights to work on Internet Explorer. I have searched for days on here and all over and I've tried some interesting things: trying to call the load event first, appending the .js file to head after page load, check for image load then run the jquery plugin, all to no avail. When the code is working the best I am getting an invalid pointer error at the line of maphilight plugin that contains the following:

wrap = $('<div></div>').css({
display:'block',
background: 'url("' + this.src + '")',
position:'relative',
padding:0,
width:this.width,
height:this.height
});

The img.src is blank until the window.onload function is called and the AJAX call is made to retrieve the base64 image. Here is the function that is called and how it displays to the page:

<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="jquery.maphilight.js" type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function(){
   $.fn.maphilight.defaults = {
    fill: true,
    fillColor: 'ffffff',
    fillOpacity: 0.5,
    stroke: true,
    strokeColor: '0055bb',
    strokeOpacity: 1,
    strokeWidth: 1,
    fade: true,
    alwaysOn: false,
    neverOn: false,
    groupBy: false,
    wrapClass: true,
    shadow: false,
    shadowX: 0,
    shadowY: 0,
    shadowRadius: 6,
    shadowColor: '000000',
    shadowOpacity: 0.8,
    shadowPosition: 'outside',
    shadowFrom: false
    }
    $('img[usemap]').maphilight();
    });

window.onload = function(){
ajaxFunction();
    }

var ttxx = new Array();
function ajaxFunction(){
        var ajaxRequest;  // Start Ajax Request

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e){
                // Something went wrong
                alert('Your browser broke!');
                return false;
            }
        }
    }

    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){        
var resp = ajaxRequest.responseText;
var spl =  resp.split('SECIMG'); // response received divide it up to get all vars 
thei = spl[0]; // get img src1 (img to be hilighted)
var rest = spl[1]; // get rest of request
var im = document.getElementById('img1');// set var for img name
im.src = thei; // set img1 src as base64 code recived
var spl3 = rest.split('eb');
var tx = spl3[0]; // get txt values
var coor = spl3[1]; // get coordinates
var ttx = tx.split(':'); 
for (u = 0; u< 8; u++){
var ne = u + 1;
ttxx[ne] = ttx[u];
} 
var mid = '100,100'; // set coordinates
var indiv = coor.split('ec');
for (i = 0; i< 8; i++){
var coord = indiv[i];
var fullcoor = coord + ',' + mid;
var upone = i + 1;
var are = document.getElementById('ar' + upone);
are.coords = fullcoor; //coordinates set
}
}

    }
    ajaxRequest.open('GET', 'testajaximg.php', true);
    ajaxRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    ajaxRequest.setRequestHeader('Authorization', 'GetCap');
    ajaxRequest.send(null); 
} // obviously set AJAX request
</script></head>

There are other functions that I won't include due to space and time constraints but those all work fine. (ie. click map area and an alert is sent)

Here is the HTML code for the body part of the page where the image is displayed (in php):

<img id="img1" border="0" usemap="#cap"><br><br> /*img put here */
<map name="cap">";
$huy = 1;
$enlop = 8;
while ($huy <= $enlop){
echo"<area shape="polygon" id="ar".$huy."\" coords="" onclick=javascript:alert('Hello'".$huy."');">";
$huy++;
}
echo"</map>";

I know the image is set because it's displayed, and I know the coordinates and areas are set because the alerts are thrown depending on where you click the image. Is the image loading too soon to be loaded by the javascript or is the javascript loading the original image src and not going to the new one? Any help will really be appreciated. You can see a live version of the code here at my website:

http://www.securacap.com/testajax2.php

Thanks again for any light that can be shed on this situation. I would prefer not to use a plugin like imagesLoaded just because I have too many plugins and scripts already! Trying to keep it as simple as possible.

share|improve this question
In addition to the things I've tried to get it to work I have tried changing the 'this.src' part of the jquery plugin that's producing the error to a blank space. This resulted in the error going away, however the image disappeared once loaded and only the hilight was shown. Also, tried putting in a static image source like "test.gif". This made it work without errors, however I want a way to have a dynamic image displayed. Thanks again for help you can provide. – Lawrence Becker Jun 5 at 19:28
Oh also it seems to work fine in ie8 not ie6 so that's what I am trying to fix. – Lawrence Becker Jun 5 at 21:56
add comment (requires an account with 50 reputation)

1 Answer

So to anyone searching and looking on how to adapt Maphilight Jquery plugin to work with dynamically displayed images under Internet Explorer, I have solved the problem and hopefully this can help someone else too.

So after thorough trial and error I discovered that the plugin sets the opacity of the orignal image to 0 thus making it invisible. Javascript was also throwing an error at me when the original image location "this.src" was seen by IE browsers (IE6 in particular). The original plugin came with a function built into it called (has_VML) to render images if VML is used (which IE6 uses). SO here is what I did with before and after (No changes to my original PHP page or AJAX call were needed).

Changes made to maphilight:

Before:

Line 248:

  // The below "this.src" under background throws an error at internet explorer when using a dynamic  image that has no original src
 wrap = $('<div></div>').css({
 display:'block',
 background: 'url("'+this.src+'")',
 position:'relative',
 padding:0,
 width:this.width,
 height:this.height
 });

After: Line 248:

  // Using the plugins function to check for VML, I set with a blank background for IE
     if (has_VML){
     wrap = $('<div></div>').css({
     display:'block',
     background: '',
     position:'relative',
     padding:0,
     width:this.width,
     height:this.height
     });
// This one is for Firefox, Chrome, Safari, etc. Image is set as background normally.
     } else {
     wrap = $('<div></div>').css({
     display:'block',
     background: 'url("'+this.src+'")',
     position:'relative',
     padding:0,
     width:this.width,
     height:this.height
     });
     }

This solves the pointer error problem, but it still doesn't display the image that was originally there because it's opacity was set to 0 by the plugin, so you have to change the opacity on the following line to render VML images differently:

Line 273: Before:

img.before(wrap).css('opacity', 0).css(canvas_style).remove(); //opacity for non VML
if(has_VML) { img.css('filter', 'Alpha(opacity=0)'); } //opacity for VML
wrap.append(img);

After:

  img.before(wrap).css('opacity', 0).css(canvas_style).remove(); //keep the same
        if(has_VML) { img.css('filter', 'Alpha(opacity=90)'); } //change to 90
        wrap.append(img);

By changing the opacity of the original image to 90% you can see the image that was originally hidden is now visible. NOTE: You can't change the opacity to 100% because the div wrapper that creates the hilight will not be visible 95% is the highest you can probably go before the hilight becomes less bright than the background. If you are happier with a lower than 90% opacity and your image still looks good, it gives you more freedom to use different hilight options below.

So now the image is shown so now the only thing left to do is replace the bottom fn.defaults function into an IF statementto render the hilight a specfic way for IE and differently for others. You will not be able to change most of the settings for the VML version of the defaults or the hilight will not be able to be seen. Still gives the effect of hilight though.

if (has_VML){ // render for VML // Note the settings are for 90% opacity set above:
    $.fn.maphilight.defaults = {
        fill: true,
    fillColor: '000000', // Color must be black or close or it won't be dark enough
    fillOpacity: 1, // full opacity
    stroke: true,
    strokeColor: '0055bb', //stroke color
    strokeOpacity: 1, //full opacity
    strokeWidth: 3, //thicker stroke to see border of hilight
    fade: true,
    alwaysOn: false,
    neverOn: false,
    groupBy: false,
    wrapClass: true,
    shadow: false,
    shadowX: 0,
    shadowY: 0,
    shadowRadius: 6,
    shadowColor: '888888',
    shadowOpacity: 0.8, //you can change this if you want
    shadowPosition: 'outside',
    shadowFrom: false
    };
    } else {// For all other browsers more options and more colors
        $.fn.maphilight.defaults = {
        fill: true,
    fillColor: 'ffffff',
    fillOpacity: .5,
    stroke: true,
    strokeColor: '0055bb',
    strokeOpacity: 1,
    strokeWidth: 1,
    fade: true,
    alwaysOn: false,
    neverOn: false,
    groupBy: false,
    wrapClass: true,
    shadow: false,
    shadowX: 0,
    shadowY: 0,
    shadowRadius: 6,
    shadowColor: '888888',
    shadowOpacity: 0.8,
    shadowPosition: 'outside',
    shadowFrom: false
    };
    }

So that's it. Don't have the plugin repull the img.src, because that will result in an error with IE6. So set the opacity of the orignal image to 90% or higher and use the built in has_VML function to create options depending on which browser is detected. I hope this helps someone in the future who is using this great plugin with dynamic images.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.