简体   繁体   English

Javascript window.prompt,变量包含反斜杠

[英]Javascript window.prompt with variable containing backslashes

I have a page with hyperlinks (naturally) that are constructed from a SQL table but some of the hyperlinks are actually network resources (ie \\server\\path). 我有一个页面,其中包含从SQL表构造的超链接(自然地),但是某些超链接实际上是网络资源(即\\ 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. 对于那些我设置了jQuery语句以查找它们,并将那些<a href>标记替换为<a onclick='window.prompt...>以便网络位置位于提示的文本框中,然后用户可以将其复制并将其粘贴到Windows资源管理器中。 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. 我知道您通常必须使用双反斜杠来转义它们,而不是手动输入路径,它们来自SQL表,并且使用变量将它们放入提示符。 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 提示起作用,但文本区域将看起来像这样\\serverfolder1folder2file.ext

Use two backslashes instead of one: \\\\server\\\\path . 使用两个反斜杠而不是一个: \\\\server\\\\path

A backslash inside JavaScript strings/Regular expressions have the special meaning of being an escape character: JavaScript字符串/正则表达式中的反斜杠具有转义符的特殊含义:

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

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

mysql_real_escape_string() is your solution. mysql_real_escape_string()是您的解决方案。

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 just thought of a simple solution that can "escape" the URL values after they've been loaded from SQL. 我只是想到了一个简单的解决方案,可以在从SQL加载URL值之后“转义” URL值。 This way I can selectively target what to escape and leave the normal http URL's as they are: 通过这种方式,我可以有选择地定位要转义的内容并保留正常的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>");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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