简体   繁体   English

如何处理javascript文件具有所使用图像的“相对”路径的情况?

[英]How do I handle situations where the javascript file has “relative” paths of images that it uses?

These paths break when I upload my media directory to S3. 当我将媒体目录上载到S3时,这些路径会中断。

Here's my process: 这是我的过程:

I have a script that uploads my entire media directory to Amazon S3 CDN. 我有一个脚本,可以将我的整个媒体目录上传到Amazon S3 CDN。 So my website downloads images/css/js from that CDN. 因此,我的网站从该CDN下载了images / css / js。

But, a lot of my plugins from JQuery have images that are "relatively" defined. 但是,我的许多来自JQuery的插件都具有“相对”定义的图像。 So when it puts to S3, that path doesn't make sense anymore. 因此,将其放到S3时,该路径不再有意义。

What do I do in this case? 在这种情况下我该怎么办? I really want to put my media folder in S3, but I just wish the paths can be kept. 我确实希望将媒体文件夹放在S3中,但我只是希望可以保留路径。

Example: 例:

/media/js/fancybox/fancybox.js 
/media/js/fancybox/fancybox-image.png

When I run this in my Apache/Django server, it works, since fancybox.js calls the fancybox-image.js, and it detects it. 当我在Apache / Django服务器上运行此程序时,它可以工作,因为fancybox.js调用fancybox-image.js并检测到它。

However, when I run it on my Amazon S3: 但是,当我在Amazon S3上运行它时:

s3.amazon.com/my_bucket/js/fancybox/fancybox.js
s3.amazon.com/my_bucket/js/fancybox/fancybox-image.png

The javascript calls this image instead: javascript改为调用此图像:

s3.amazon.com/my_bucket/fancybox-image.png

Define a URLbase variable in your JS so that in your development environment it's something like URLbase = "http://localhost" and on production it's URLbase = "http://s3.amazon.com/my_bucket" 在您的JS中定义一个URLbase变量,以便在您的开发环境中类似于URLbase = "http://localhost"而在生产环境中则URLbase = "http://s3.amazon.com/my_bucket"

Then when you're defining URLs you can prepend URLbase to the location, or define a utility function that will prepend the URLbase to the path (this would make it more flexible if you've got a few base URLs you want to mix in at once.) 然后,当您定义URL时,可以将URLbase放在该位置之前,或者定义一个实用程序函数,该功能会将URLbase放在该路径之前(如果要混合一些基本URL,这将使其更加灵活。一旦。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM