带有包含反斜杠的变量的Javascript window.prompt

时间:2011-09-20 16:08:28

标签: javascript jquery prompt backslash

我有一个带有超链接(自然)的页面,它是从SQL表构建的,但是某些超链接实际上是网络资源(即\ server \ path)。对于那些我设置jQuery语句来查找它们并用<a href>替换这些<a onclick='window.prompt...>标签,以便网络位置将在提示的文本框中,然后用户可以复制它并将其粘贴到Windows资源管理器中。问题是,所有反斜杠都被删除了。我知道你通常必须用双反斜杠来逃避它们我没有手动放置路径,它们来自SQL表,我使用变量将它们放入提示符中。有谁能说出是否有解决方案?

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

完全相同

3 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:0)

mysql_real_escape_string()是您的解决方案。

echo mysql_real_escape_string("hello\world");

输出

hello\\world

因此,在将服务器值传递回客户端之前,请先将其转义。

答案 2 :(得分:0)

我只想到一个简单的解决方案,可以在从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>");
        }