简体   繁体   English

在Javascript中获取相对CSS URL路径

[英]Get relative CSS URL path in Javascript

NOTE: I finally found an exact duplicate question and good answer at: Get script path 注意:我终于找到了一个完全重复的问题和答案: 获取脚本路径

I have a background-image defined with a path relative to my css file. 我有一个背景图像定义了相对于我的CSS文件的路径。

.my-image-class {
  background-image: url("../images/my_bg_image.png");
}

I would like change this path in JavaScript so that it now points to ../images/my_updated_bg_image.png . 我想在JavaScript中更改此路径,以便它现在指向../images/my_updated_bg_image.png However simply changing the style declaration in JavaScript unfortunately changes the meaning of the relative path from the CSS location to the HTML location. 但是,简单地更改JavaScript中的样式声明会更改从CSS位置到HTML位置的相对路径的含义。

Is there a way to extract an absolute URL from a relative CSS URL? 有没有办法从相对CSS URL中提取绝对URL?

If not is there a way to extract an absolute URL of the source of a currently running JavaScript function? 如果没有,有没有办法提取当前运行的JavaScript函数的源的绝对URL? This would give me the same information because my JS files are also relative to the images files. 这会给我相同的信息,因为我的JS文件也与图像文件有关。

Other solutions? 其他方案?

This is a similar question to javascript - How to use a image file relative to a url path? 这是一个类似于javascript的问题- 如何使用相对于url路径的图像文件? but the class swapping trick suggested there won't work for me because I actually have a variable list of these background images. 但是类交换技巧表明这对我不起作用,因为我实际上有一个这些背景图像的变量列表。

EDIT: Background info. 编辑:背景信息。 The web app (Django based) is serving HTML content entirely separately from static js, image, and css files. Web应用程序(基于Django)完全独立于静态js,image和css文件提供HTML内容。 The static content is in a 静态内容在

static/
  js/
  css/
  images/

file structure. 文件结构。 So I know the relative relationships among these files. 所以我知道这些文件之间的相对关系。 However the absolute URL's to the HTML content and the static content will likely be changing. 但是,HTML内容和静态内容的绝对URL可能会发生变化。 So I don't want mix in hard coded URL's for HTML content into my js files if I can possibly avoid it. 所以我不希望将HTML内容的硬编码URL混合到我的js文件中,如果我可以避免它。 I'm getting the Django template from a 3rd party, so I'd also like to avoid editing that as well. 我从第三方获得了Django模板,所以我也想避免编辑它。

URLs in CSS stylesheets are relative to the CSS file in which they appear (even if @import ed). CSS样式表中的URL与它们出现的CSS文件相关(即使是@import ed)。 If they appear in HTML, they are relative to the base URL of the HTML file which is typically the URL of the HTML file, unless there is a <base href=...> element in the HTML. 如果它们以HTML格式显示,则它们相对于HTML文件的基本URL(通常是HTML文件的URL),除非HTML中有<base href=...>元素。

So you need to figure out the absolute URL against which the relative URL is resolved and then calculate a relative URL for your image. 因此,您需要找出解析相对URL的绝对URL,然后计算图像的相对URL。

You might try seeing if the old image is being served from multiple different places. 您可以尝试查看是否从多个不同的地方提供旧图像。 Lots of web servers accumulate cruft and perhaps you're just putting the new image in a logical, but still wrong, place. 很多网络服务器累积起来,也许你只是把新图像放在一个逻辑但仍然错误的地方。

EDIT: 编辑:

There are a variety of options in between absolute and path relative ../foo style URLs. 绝对和路径相对../foo样式URL之间有多种选择。

Absolute paths: /images/myImage.png . 绝对路径:/ /images/myImage.png

Protocol relative URLs: //my-domain.com/image/myImage.png 协议相对URL: //my-domain.com/image/myImage.png

Either of these might let you strike a better balance between specifying as much as you need but no more. 这些中的任何一个都可以让您在指定所需数量之间取得更好的平衡,但不能更多。

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

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