问题描述
- Javascript, html特殊字符问题:当这个@Name里有单引号,或者双引号,IE上现在在报错,怎么让他不报错
-
Javascript, html特殊字符问题:当这个@Name里有单引号,或者双引号,IE上现在在报错,怎么让他不报错
比如
test'testtest"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了。
解决方案二:
到底是报错还属不报错。
{@Name}
这个是什么js模板还是服务器模板?如果是模板,实际上在运行的时候dom已经被替换为实际的name的值了。不存在你说的问题。
至于引号转义,这个你可以用htmlencode编码下。
解决方案三:
那你想要表达什么呢,
解决方案四:
怎么整?我不是很明白js膜拜还是服务器模板。 怎么用htmlencode? 能具体点吗?我没怎么整过javascript.谢谢
<?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="alert('{@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>
解决方案五:
onclick="alert('{@Name}')"换成onclick="alert('test"test')"报错吗?
估计也是报错的,应该是不能有‘,“吧,非要有的话那就先转换成其他字符,然后再转回来
解决方案六:
onclick="alert('test"test')"这个不报错。 这个onclick="alert('test'test')"报错。怎么解决?
解决方案七:
使用转义',比如onclick="alert('test'test')"
解决方案八:
the 'test'test'来自于
这个是用户填的,我并不知道用户何时填什么字符。这个用javascript怎么出来呢。