kartograph / kartograph.js

UNMAINTAINED Open source JavaScript renderer for Kartograph SVG maps
http://kartograph.org
GNU Lesser General Public License v3.0
1.51k stars 228 forks source link

Allow Cross Origin SVG Loading #18

Closed gka closed 11 years ago

gka commented 12 years ago

In some situations it is not possible to put the svg map files on the same origin on which the map is embedded. By default, xhr loading of resources from other domains is not possible, which is why the w3c introduced the CORS protocol.

Kartograph.js should be able to load maps from CORS enabled origins. jQuery already supports CORS.

I think there are different ways of handling this problem.

(1) Completely ignore the problem of loading svg assets and add a map.setMap( svgAsDomOrString ) interface. This way, users are forced to find a proper way of getting to their svgs. Of course, this complicates the setup significantly for some users.

(2) Try to wrap the CORS logic of jquery:

map.loadMap({
    url: "http://anotherdomain.org/map.svg",
    origin: "http://mydomain.org",
    success: function() {
        // proceed
    }
});

(3) Of course, we can also allow both (1) and (2).

(4) Idea for situations where CORS is no option at all (say for IE7): Since SVG is a simple plain/text resource it could easily be loaded through a JSONP api. Kartograph would for instance expect that the result of the request is a single string representing the svg file.

map.loadMap({
    url: "http://anotherdomain.org/svgapi/?load=map.svg",
    format: "jsonp",
    success: function() {
        // proceed
    }
});
zzolo commented 11 years ago

I think the best overall approach is to support direct string input. This allows users to get their SVG from a different domain however they want.

gka commented 11 years ago

Yep, nice idea. Will add the following API:

map.setMap(svgString, opts);
gka commented 11 years ago

fixed via 33da52ccff9f756bac76d4edf3bde782eb1954c3

zzolo commented 11 years ago

Awesome! Thanks.