admin管理员组文章数量:1390438
In the little JavaScript I wrote, the 'src' attribute of 'img' element accepted and displayed the image only if I changed '\' to '/' in the relative path of the image.
Why is it so?
<html>
<body>
<img id="image" src="F:\wallpapers\other\black-and-white-lion-chess-hd-531078.jpg">
//why did '\' work here?
<script>
document.getElementById("image").src="F:/wallpapers/other/clouds_nature_skyscapes.jpg";
//why didn't '\' work here? Why did I have to use '/' ?
</script>
</body>
</html>
In the little JavaScript I wrote, the 'src' attribute of 'img' element accepted and displayed the image only if I changed '\' to '/' in the relative path of the image.
Why is it so?
<html>
<body>
<img id="image" src="F:\wallpapers\other\black-and-white-lion-chess-hd-531078.jpg">
//why did '\' work here?
<script>
document.getElementById("image").src="F:/wallpapers/other/clouds_nature_skyscapes.jpg";
//why didn't '\' work here? Why did I have to use '/' ?
</script>
</body>
</html>
Share
Improve this question
asked Mar 9, 2014 at 12:08
NikhilNikhil
6,66110 gold badges35 silver badges72 bronze badges
1
-
You probably want
file://
URLs; they're better supported, and there's an RFC I can point to that defines their syntax. – David Ehrmann Commented Mar 9, 2014 at 17:07
1 Answer
Reset to default 6A single \
character in a Javascript string is used to "escape" the next character, e.g. \n
is used for the newline character. See this
In your example, if you want to use windows path names, use \\
as path separator, like so: F:\\wallpapers\\other\\clouds_nature_skyscapes.jpg
本文标签:
版权声明:本文标题:image - Why use forward slash '' instead of backward slash '' in relative paths in JavaScript? - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744642922a2617227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论