簡體   English   中英

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

[英]Javascript window.prompt with variable containing backslashes

我有一個頁面,其中包含從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

使用兩個反斜杠而不是一個: \\\\server\\\\path

JavaScript字符串/正則表達式中的反斜杠具有轉義符的特殊含義:

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

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

mysql_real_escape_string()是您的解決方案。

echo mysql_real_escape_string("hello\world");

輸出

hello\\world

因此,在將服務器值傳遞回客戶端之前,只需對其進行轉義即可。

我只是想到了一個簡單的解決方案,可以在從SQL加載URL值之后“轉義” 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>");
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM