Category Archives: micro tutorial

Change the sea/ocean color of OpenLayers (OSM) map

The ocean is blue!

Yeah, that should sound OK, because as we all know the ocean is blue, but here I mean the ocean is blue on the maps of OpenStreetMap.org. This map uses pre-generated tiles with parts of the world map and using a tool such OpenLayers javascript library you can construct a geo map. The problem is that those tiles are always with blue ocean, as you can see on the picture below:

a openstreetmap tile with part of east asia
a openstreetmap tile with part of east asia

Well those are generated, how can I change the ocean color?

There is no way to change it, except you modify the .xml file from which those tiles are generated, but so far I haven’t done it.

Since I need a vector (thematic) map, such as this on http://thematicmapping.org/ with geoJSON formated borders I noticed that one you’ve a vector map, there’s no loading of the tiles and thus – no blue color of the ocean. You can see an example here:

emq26y57vz

jQuery: check whether image is loaded

Wait for the image!

This is common task. You’d like to load an image and then show it. With jQuery that is simple enough. Let’s assume this code:

<img src="image.jpg" />
<div>displayed after the image is loaded</div>

This is OK if the div is hidden with jQuery you can show it with adding this code in your javascript:

<script>
    $('img').load(function() {
        $('div').show();
    });
</script>

cancel bubbling on element click with jQuery

When you click over an element

This is easy, on the element can be attached event listener an such event listener is built in as the click event is. It’s simply like that:

<div click="func1()"></div>

This with jQuery

A bit different with jQuery, but with the same effect is:

$('div').click(func1);

When there is a bubbling?

Well, if you just copy paste this code in your page:

$(document).click(function() { 
    console.log('clicked on the document');
});
$('div').click(function() { 
    console.log('clicked on the div');
});

Note: if you’re using Firefox keep the console.log function, in other case you may replace it with either alert or $.log from the recent jQuery plugin i wrote.

Now if you click over the div element you get both messages, cause there is no bubble canceling. If you’d like to add such canceling, just add this code to the click callback for the div element:

return false;

and now the code should look like:

$(document).click(function() { 
    $.log('clicked on the document'); 
});
$('div').click(function() { 
    $.log('clicked on the div'); return false;
});

javascript: what is typeof typeof

typeof

If you’ve ever heard about typeof operator in javascript than, how about those few questions:

  1. what is typeof typeof ‘string’ ?
  2. what is typeof typeof?

Answers

The first one is “string”. Yes if there you have the following code:

typeof something_undefined

that gives of course undefined, but if there is typeof typeof something_undefined the answer is string, because “udefined” itself is a string.

The second question is simple javascript error, there is no case with typeof typeof only.

jQuery debug plugin

Console.log()

Almost everybody using Firebug extension of Firefox is familiar with console object and in common with console.log method, who’s taking an object parameter and dumps it into the Firebug console. It’s perfect for debugging and it’s really useful.The problem is that if you don’t have Firebug installed or it’s disabled the console object is undefined. The same may occur if you’re using other browser. That’s why I decided to make a simple plugin for jQuery with the $.log interface which checks for the console and opera objects and if they don’t exists just alert the message. It’s nothing special.

Continue reading jQuery debug plugin →