什么正在取代我的'以及“;javascript代码中使用'和&”;

What is replacing my ' and " characters in javascript code with ' and "?

本文关键字:amp#39 amp 代码 取代 我的 什么 以及 javascript      更新时间:2023-09-26

源(.Net 3.5)

  output.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('"In order to prevent loss of your work, you may only use the progress grids within the same section.'");return false");

在浏览器中渲染:

<a onclick="alert(&quot;In order to prevent loss of your work, you may only use the progress grids within the same section.&quot;);return false" href="myPage.aspx#1">

这意味着JavaScript不会执行。

类似地,如果我用单引号替换内部引号,它就会转义为';

在IIS版本7 上运行

它似乎是独立于浏览器的(IE 8,9 FF 15,Safari 7,Chrome)

导致这种情况发生的可能是IIS,也可能是.Net配置。

当然,有趣的是它昨晚运行良好,现在坏了。

有什么办法可以抑制这种行为吗?

这意味着JavaScript不会执行。

不,不是。它防止数据中的"字符终止HTML属性值。

HTML解析器将HTML解码为文本(将&quot;转换为",依此类推),并将其传递给JavaScript解析器。

有什么办法可以抑制这种行为吗?

AddAttribute的第三个参数(fEncode)设置为false。这将破坏您的HTML(从而破坏您的脚本)。