开发者

# character in path of javascript reference

开发者 https://www.devze.com 2022-12-12 20:20 出处:网络
I have a web page that pulls in several javascript files in the HEAD section.The path to one of those files is generated dynamically, and I\'m seeing an issue with s开发者_运维问答ome paths that inclu

I have a web page that pulls in several javascript files in the HEAD section. The path to one of those files is generated dynamically, and I'm seeing an issue with s开发者_运维问答ome paths that include the # character. For example, the following path does not resolve correctly, and therefore the .js file does not load (even though I've verified that it exists):

<script src="\\remote_machine\share\test\this is #3 test\test.js"></script>

I'm wondering what the special meaning of '#3' is in this situation. I've tried replacing the # character with the equivalent html entity, like so:

<script src="\\remote_machine\share\test\this is &#35;3 test\test.js"></script>

but that does not resolve the issue. Further, I notice that the following path does resolve correctly (assuming the file exists):

<script src="\\remote_machine\share\test\this is # test\test.js"></script>

Could anyone explain the meaning of '#3' in the context of html as described? What's the recommended fix?

EDIT: I've tried replacing the '#' with "%23" in all three examples above. The first two remain broken, and the third no longer works. If I additionally replace all spaces with %20, I get the same result.


Apart from the issue with the hash and spaces (which should indeed be encoded as %23 and %20 respectively), you've also got backslashes. Backslashes aren't directory separators in URLs. A URL is not the same as a filename, and what you've got there isn't even a filename, it's a UNC path, for local-area Windows networking.

There is no standard way to embed a UNC path in a URL; Windows networking has no place on the web and you shouldn't include references to resources on a Windows network in a web page. That js needs to be on a proper web server that you can reference with normal HTTP URLs.

If you must try:

file://remote_machine/share/test/this%20is%20%23%20test/test.js     # works on IE, Chrome, Opera
file://///remote_machine/share/test/this%20is%20%23%20test/test.js  # works on IE, Firefox

(Neither works on Safari.)


Try replacing your # with %23. And while you're at it, I'd replace your spaces with %20 as well


You need to use URL encoding:

<script src="\\remote_machine\share\test\this%20is%20%23%20test\test.js"></script>

And try the file URI scheme:

<script src="file:////remote_machine/share/test/this%20is%20%23%20test/test.js"></script>
0

精彩评论

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