Using relative path in javascript to set google map icon

I set the icon image on my google map using the 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

Can I use a relative URL? Will the path from js script be: ../ images / icon1.png I tried everything I could come up with. Google map is in the WordPress plugin.

WordPress has many PHP functions for defining file paths. I could save the file path in PHP and then send it to a javascript script, but I hope there will be a better, easier way. I don’t want to hardcode the path to the image file, as this will be another thing that could go wrong. Perhaps there are javascript functions that will work in this case.

Thanks.

+4
source share
1 answer

Relative paths should work fine, but they should refer to the path of the map page, not the JS script. If you use a script map on different pages that require different relative paths, the best option is to start with a slash (for example, /wp-content/plugins/my_plugin/images/icon1.png ).

+7
source

All Articles