In CSS, any image path is relative to the CSS file location.
f.ex if I put the CSS file in /media/css/mystyles.css
and use something like
.background:url(../images/myimage.jpg);
The browser will look for the image in /media/images/myimage.jpg
which makes sense.
Is it possible to do the same thing in javascript?
F.ex if I include /media/js/myscript.js
and put this code in there:
var img = new Image();
img.src = '../images/myimage.jpg';
Th image is not found, since browser is using the HTML file as a starting point, instead of the script location. I would like to be able to use the script location as a starting point, just like CSS does. Is this possible?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…