简体   繁体   中英

Javascript converts back-slash characters into forward-slash characters

When using Javascript to redirect the user to another page (assigning to document.location), I've noticed that Javascript will convert any back-slash character you pass into the URL into a forward-slash character.

eg, given this sample HTML document

<html>
<head>
</head>

<body>
    <input type="button" id="takemeto" value="hello" onclick="document.location = '/hello\\world';" />
</body>
</html>

The expected URL it should attempt to load is '/hello\\world', however, the URL it actually attempts to load is '/hello/world'. This problem only seems to occur in Safari as Firefox seems to correctly maintain my URL.

My question is, is there a way to perform a redirect in Javascript that is cross-browser and that will maintain the back-slash characters in my URL?

您可以尝试对字符进行百分比编码

document.location = '/hello%5Cworld'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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