Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Relative Path in Javascript to Set Google Map Icon

I've set the icon image in my Google map using an absolute url as follows:

 icon: 'http://mysite/wp-content/plugins/my_plugin/images/icon1.png'

I set the image in this script: http://mysite/wp-content/plugins/my_plugin/js/myscript.js

Is it possible to use a relative URL? Would the path from the js script be: ../images/icon1.png I've tried every way that I can think of. The Google map is in a WordPress plugin.

WordPress has a lot of PHP functions for determining file paths. I could save file path in PHP and then send it to the javascript script, but I hope that there's a better, simpler way. I don't want to hardcode the image file path as it will be one more thing that can go wrong. Perhaps, there are javascript functions that would work in this case.

Thank you.

like image 661
Laxmidi Avatar asked Oct 07 '11 18:10

Laxmidi


1 Answers

Relative paths should work fine, but they need to be relative to the path of the page with the map, not relative to the JS script. If you're using the map script on different pages requiring different relative paths, than a path starting with a slash (e.g. /wp-content/plugins/my_plugin/images/icon1.png) is the best option.

like image 192
nrabinowitz Avatar answered Nov 15 '22 12:11

nrabinowitz