Javascript window.prompt 变量包含反斜杠

发布于 2024-12-05 07:52:08 字数 641 浏览 0 评论 0原文

我有一个包含超链接(自然地)的页面,这些超链接是从 SQL 表构建的,但某些超链接实际上是网络资源(即 \server\path)。对于这些,我设置了一个 jQuery 语句来查找它们,并将这些 标记替换为

else if (($link.length > 0) && ($link.substring(0, 4) != "http")) {
  $('.linktext', $this.closest('tr')).after("<span><a href='#link' onclick='window.prompt(\"This resource is located on a network drive and is not accessible via the web browser. Please copy the link and paste into Windows Explorer.\",\""+$link+"\");'>Text</a></span>");
}

提示有效,但文本区域看起来与此完全相同 \serverfolder1folder2file.ext

I have a page with hyperlinks (naturally) that are constructed from a SQL table but some of the hyperlinks are actually network resources (i.e. \server\path). For those I set up a jQuery statement to find them and replace those <a href> tags with <a onclick='window.prompt...> so that the network location will be in the textbox of the prompt then users can copy it and paste it into Windows Explorer. The problem is, all the backslashes are being removed. I know you usually have to escape them with double backslashes I'm not putting the paths in manually, they're coming from the SQL table and I'm putting them into the prompt using a variable. Can anyone tell if there's a solution?

else if (($link.length > 0) && ($link.substring(0, 4) != "http")) {
  $('.linktext', $this.closest('tr')).after("<span><a href='#link' onclick='window.prompt(\"This resource is located on a network drive and is not accessible via the web browser. Please copy the link and paste into Windows Explorer.\",\""+$link+"\");'>Text</a></span>");
}

The prompt works but the text area will look exactly like this \serverfolder1folder2file.ext

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁轨上的流浪者 2024-12-12 07:52:08

使用两个反斜杠而不是一个:\\server\\path

JavaScript 字符串/正则表达式中的反斜杠具有作为转义字符的特殊含义:

var stringWithNewLine = "This
doesn't work"; //Error

var stringWithNewLine = "This\ndoes work"; //Escaped new-line feed.

Use two backslashes instead of one: \\server\\path.

A backslash inside JavaScript strings/Regular expressions have the special meaning of being an escape character:

var stringWithNewLine = "This
doesn't work"; //Error

var stringWithNewLine = "This\ndoes work"; //Escaped new-line feed.
随梦而飞# 2024-12-12 07:52:08

mysql_real_escape_string() 是您的解决方案。

echo mysql_real_escape_string("hello\world");

因此,在将服务器值传递

hello\\world

回客户端之前,只需转义服务器值即可。

mysql_real_escape_string() is your solution.

echo mysql_real_escape_string("hello\world");

outputs

hello\\world

So just escape your server values before your pass them back to the client.

陪我终i 2024-12-12 07:52:08

我只是想到了一个简单的解决方案,可以在从 SQL 加载 URL 值后对其进行“转义”。这样我就可以有选择地定位要转义的内容并保留正常的 http URL:

else if (($link.length > 0) && ($link.substring(0, 4) != "http")) {
            $linkescaped = $link.replace(/\\/g, "\\\\");
            $('.filter', $this.closest('tr')).after("<span><a href='#link' onclick='window.prompt(\"This resource is located on a network drive and is not accessible via the web browser. Please copy the link and paste into Windows Explorer.\",\""+$linkescaped+"\");'></a></span>");
        }

I just thought of a simple solution that can "escape" the URL values after they've been loaded from SQL. This way I can selectively target what to escape and leave the normal http URL's as they are:

else if (($link.length > 0) && ($link.substring(0, 4) != "http")) {
            $linkescaped = $link.replace(/\\/g, "\\\\");
            $('.filter', $this.closest('tr')).after("<span><a href='#link' onclick='window.prompt(\"This resource is located on a network drive and is not accessible via the web browser. Please copy the link and paste into Windows Explorer.\",\""+$linkescaped+"\");'></a></span>");
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文