开发者

Using a Relative Path in Javascript to Set Google Map Icon

开发者 https://www.devze.com 2023-04-11 06:55 出处:网络
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'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 th开发者_Go百科is 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消