views:

308

answers:

2

Hi all:

This is uni assignment and I have already done some stuff. Please go to the password protected directory on : my server

Enter username "uts" and password "10479475", both without quotes, into the prompt and you shall be able to see the webpage.

Basically, if you hover your mouse on top of the contents in worldmap to the upperleft corner, you can see the underneath area is "highlighted" by a gray region and a red border. This is done using one jQuery plugin : at here

This part works fine, however, after I use jQuery to load the specific continent map asynchronously, the newly loaded image cannot work correctly. Tested under Firebug, I can see the plugin doesn't "like" the new image cause I cannot find the canvas or other auto-generated stuff which can be founded around the worldmap.

All the functionality is done in master.js, I believe you can just download a copy and check the code there. I do hope that I have followed the tutorials on the plugin's doc page, but I just cannot get through the final stage.

Code used for worldmap in html:

<img id="worldmap" src="./img/world.gif" alt="world.gif" 
width="398" height="200" class="map" usemap="#worldmap"/>
<map name="worldmap">
<area class='continent' href="#" shape="poly" title="North_America" 
coords="1,39, 40,23, 123,13, 164,17, 159,40, 84,98, 64,111, 29,89" />
    </map>

Code used for worldmap in master.js

//when DOM is ready, do something
$(document).ready(function()
{   
    $('.map').maphilight(); //call the map highlight main function
}

On contrast, code used for specific continent map:

//helper function to load specific continent map using AJAX
function loadContinentMap(continent)
{
$('#continent-map-wrapper').children().remove();    //remove all children     nodes first

//inspiration taken from online : http://jqueryfordesigners.com/image-loading/
$('#continent-map-wrapper').append("<div id='loader' class='loading'><div>");

var img = new Image();  // wrap our new image in jQuery, then:

// once the image has loaded, execute this code
$(img).load(function () 
{    
  $(this).hide();   // set the image hidden by default

  // with the holding div #loader, apply:
  // remove the loading class (so no background spinner),
  // then insert our image
  $('#loader').removeClass('loading').append(this);

  // fade our image in to create a nice effect
  $(this).fadeIn();
}).error(function () 
{
    // if there was an error loading the image, react accordingly
    // notify the user that the image could not be loaded
    $('#loader').removeClass('loading').append("<h1><div class='errormsg'>Loading image failed, please try again! If same error persists, please contact webmaster.</div></h1>");
})
//set a series of attributes to the img tag, these are for the map high lighting plugin.
.attr('id', continent).attr('alt', '' + continent).attr('width', '576').attr('height', '300')
.attr('usemap', '#city_' + continent).attr('class', 'citymap').attr('src', './img/' + continent + '.gif');
// *finally*, set the src attribute of the new image to our image

//After image is loaded, apply the map highlighting plugin function again.
$('.citymap').maphilight();

$('area.citymap').click(function()
{
    alert($(this).attr('title') + ' is clicked!');
});

}

Sorry about the messy code, havn't refactored it yet.

I am wondering why the canvas disappers for the continent map. Did I do anything wrong.

Any hint is much appreciated and thanks for any suggestion in advance!

+1  A: 

This all works well if I type $('.citymap').maphilight(); into Firefox's JavaScript console.
You are calling it too early - it looks like you call it before adding the <img> to the DOM.

Kobi
This is right! I just found out this is the cause. so, let me try to delay the calling for some time.
Michael Mao
Well, it seems the plugin only accepts those "hard-coded" in html when DOM is ready. If jQuery dynamically manipulate the DOM, the plugin cannot see it as we do... Or at least this is what I reckon. So I wonder, if a jQuery load() function would do since that loads html into one node in DOM tree.
Michael Mao
Are you sure of that? What if you call `maphilight` after the image is displayed? (after `fadeIn`, for example?) I can't test it myself atm...
Kobi
@Kobi : yeah, I am sure of that. I even delayed the highlight function for 5 sec after the image is displayed on screen. The plugin still cannot get the job done. So I am afraid I have to switch to other means to overcome this difficulty.
Michael Mao
Another weird point - why are you loading this dynamically? You only have very few images, you can load them from the start (in your HTML). The image maps are there anyway...
Kobi
@Kobi : That's right, I should have loaded all images and use jQuery only to manipulate the display... Thanks for your explaination.
Michael Mao
A: 

Looking at the code you provided, the problem does seem to be that you're trying to call maphilight before the element it's called on exists in the DOM.

The image is only added to the DOM after $(img).load has run, so $('.citymap').maphilight(); should probably go in there.

David