程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> javascript-Javascript, html特殊字符問題:當這個@Name裡有單引號,或者雙引號,IE上現在在報錯,怎麼讓他不報錯

javascript-Javascript, html特殊字符問題:當這個@Name裡有單引號,或者雙引號,IE上現在在報錯,怎麼讓他不報錯

編輯:編程綜合問答
Javascript, html特殊字符問題:當這個@Name裡有單引號,或者雙引號,IE上現在在報錯,怎麼讓他不報錯

Javascript, html特殊字符問題:當這個@Name裡有單引號,或者雙引號,IE上現在在報錯,怎麼讓他不報錯
比如
test'test

test"test
注意:若我輸入普通的字符串,它能正常工作。

 <?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <table cellpadding="0" cellspacing="0" class="LineItem">
      <xsl:for-each select="//Cluster">
        <xsl:sort select="@Name" order="ascending" data-type="text" />
        <tr>
          <td width="20" nowrap="true">
            <img src="../images/spacer.gif" />
            <img src="../images/cluster.gif" />
          </td>
          <td class="headerCell"  width="100%" onclick="SelectSurveyItem('{@ClusterId}','{@Name}')">
            <div class="pseudoLink">
              <xsl:value-of select="@Name" />
            </div>
          </td>
        </tr>
        <tr>
          <td colspan="4" class="break"></td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>  
</xsl:stylesheet> 

Maybe I'm just thinking about this too hard, but I'm having a problem figuring out what escaping to use on a string in some JavaScript code inside a link's onClick handler. Example:

 <td class="headerCell"  width="100%" onclick="SelectSurveyItem('{@ClusterId}','{@Name}')">
            <div class="pseudoLink">
              <xsl:value-of select="@Name" />
            </div>
          </td>

The '{@Name}' where template substitution occurs. My problem is that the item name can contain any character, including single and double quotes. Currently, if it contains single quotes it breaks the JavaScript code.

My first thought was to use the template language's function to JavaScript-escape the item name, which just escapes the quotes. That will not fix the case of the string containing double quotes which breaks the HTML of the link. How is this problem normally addressed? Do I need to HTML-escape the entire onClick handler?

If so, that would look really strange since the template language's escape function for that would also HTMLify the parentheses, quotes, and semicolons...

This link is being generated for every result in a search results page, so creating a separate method inside a JavaScript tag is not possible, because I'd need to generate one per result.

Also, I'm using a templating engine that was home-grown at the company I work for, so toolkit-specific solutions will be of no use to me.
http://stackoverflow.com/questions/97578/how-do-i-escape-a-string-inside-javascript-code-inside-an-onclick-handler

這個英文版的問題差不多就是我的問題,但是我不知道如何破?

最佳回答:


在進行頁面load時,就把test'test之類的變量判斷是否有',若有,就用&pros;替換。這樣就KO了。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved