Question

I am loading multiple Google maps in JavaScript, but the only way I was able to accomplish this in JavaScript was to use "eval".

I've read that eval is insecure. Can the following line be simplified or done another way?

eval('var map' + uin + ' = ' + 'new google.maps.Map(map_canvas, map_options);');
Was it helpful?

Solution

var maps = {};
maps[uin] = new google.maps.Map(map_canvas, map_options)

Using objects is fun!

OTHER TIPS

There's no need to create a dynamic variable name.

How about creating an array of maps?

var maps  = [];

maps[ 0 ] = new google.maps.Map(map_canvas, map_options);

maps[ 1 ] = new google.maps.Map(map_canvas, map_options);

But it looks like you're using the same map_canvas for all these maps – that's probably going to screw something up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top