开发者

Image through Script, How to add variable to URL

I'm sure it's pretty straightforward since someone just solved a similar problem I had. I have a folder full of city images and need users to see the image corresponding to their city.

Right now I'm getting the users location without problem using 开发者_如何学编程"geoip_city()" but I don't quite know how to integrate it into the URL of the image. All images have the following format: New York.jpg, Boston.jpg so I just need to make the script put the location before .jpg

This is what I'm trying now:

<img src="blank.png" id="image" >

<script type="text/javascript">
document.getElementById('image').src = "http://www.chusmix.com/Imagenes/grupos/' + geoip_city() + '.jpg";
</script>

I believe I'm just messing with the quotes or something similar. Could anyone tell me what I'm doing wrong? Also this is where I'm testing it: http://chusmix.com/?page_id=1770

Thanks


You must use the same kind of quotes.

Also, assuming your images have spaces in their filenames, you have two approaches:

Replace the results of geoip_city() with underscores (or another character):

geoip_city().replace('_', ''); // AND change your filenames.

Or, you can use encodeURI() to replace spaces with URL-friendly characters:

encodeURI(geoip_city());

You won't have to change your filenames if you do encode the city name.


Also, your quotes are not correct. You need:

document.getElementById('image').src = "Imagenes/grupos/" + geoip_city() + ".jpg";

I hope your site is catered to a limited amount of cities - that would be a lot of images for you to create...

(removed the spaces thing because I thought your image names had no spaces - need new glasses :))


The quote types need to match, that's all, like this:

document.getElementById('image').src="Imagenes/grupos/" + geoip_city() + ".jpg";
                                                      ^  double quotes   ^

They could be single as well, whatever matches the wrapping quotes is the important part.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜