我使用HtmlAgilityPack和C#来转换旧的IE标签以及Javascript以与其他浏览器兼容。这里有一个例子:使用HtmlAgilityPack设置InnerHtml属性会产生意想不到的结果
旧代码:
<script for="thisForm" event="onsubmit()" language="JScript">
var Checked = false
var Counter = 0
for (;Counter < this.choice.length; Counter++)
{
if (this.choice[Counter].checked)
{
Checked = true
this.action = this.choice[Counter].value
}
}
if (!Checked)
{
alert ("Please make a selection")
return false
}
</script>
我转换为:
<script ftype="text\JScript">
function thisForm_onsubmit(el)
{
var Checked = false
var Counter = 0
for (;Counter < el.choice.length; counter++)
{
if (el.choice[counter].checked)
{
checked = true
el.action = el.choice[counter].value
}
}
if (!checked)
{
alert ("please make a selection")
return false
}
}
</script>
我上面的,活动中移除的内容,和语言的脚本标签属性,添加了type = “text/JScript”属性并将javascript包装成功能代码。
我这样做只需添加HtmlNode属性,然后替换InnerHtml属性值。到目前为止,它工作正常,直到我遇到上述功能。不知何故,而不是给我上面的结果,我得到如下:
<script type="text/JScript">
function thisForm_onsubmit(el)
{
var Checked = false
var Counter = 0
for (;Counter < el.choice.length; counter++)
{
if (el.choice[counter].checked)
{
checked = true
el.action = el.choice[counter].value
}
}
if (!checked)
{
alert ("please make a selection")
return false
}
}
el.choice.length;="" counter++)="" {="" if="" (el.choice[counter].checked)="" {="" checked="true" el.action="el.choice[Counter].value" }="" }="" if="" (!checked)="" {="" alert="" ("please="" make="" a="" selection")="" return="" false="" }="" }=""></ el.choice.length; counter++)
{
if (el.choice[counter].checked)
{
checked = true
el.action = el.choice[counter].value
}
}
if (!checked)
{
alert ("please make a selection")
return false
}
}
></script>
奇怪的部份,我分配给的innerHTML文本是正确的,但scriptNode.InnerHtml表现出不同的价值
这里是我的C#代码:
if (scriptNode.Attributes["for"] != null)
{
{
if (scriptNode.Attributes["for"] != null)
ctrl = scriptNode.Attributes["for"].Value;
if (scriptNode.Attributes["event"] != null)
evt = scriptNode.Attributes["event"].Value;
if (scriptNode.Attributes["type"] != null)
typ = scriptNode.Attributes["type"].Value;
if (scriptNode.Attributes["language"] != null)
lang = scriptNode.Attributes["language"].Value;
if (scriptNode.InnerHtml != null)
code = scriptNode.InnerHtml;
func_name = ctrl + "_" + evt;
if (ctrl != "window")
new_script = Environment.NewLine + "function " + RemoveBrackets(func_name) + "(el)" + Environment.NewLine;
else
new_script = Environment.NewLine + "function " + AddBrackets(RemoveBrackets(func_name)) + Environment.NewLine;
new_script += "{" + Environment.NewLine;
new_script += "\r\n" + ReplaceThis(sFile, ctrl, evt, code, "this", "el") + "\r\n" + "}" + "\r\n";
//remove for and event attributes
scriptNode.Attributes["for"].Remove();
scriptNode.Attributes["event"].Remove();
//remove depraciated "language" attribute
//and replace it with "type" attribute
if (scriptNode.Attributes["language"] != null)
scriptNode.Attributes["language"].Remove();
if (scriptNode.Attributes["type"] == null)
scriptNode.Attributes.Add("type", "text/" + lang);
//replace old javascript with a function code
//HERE new_script variable contains the correct value but when I check scriptNode.InnerHtml after assignment, it shows the messed up code.
scriptNode.InnerHtml = new_script;
这是非常奇怪的,我似乎无法找到解决方案。
我已经采用的HTMLEncode
试图scriptNode.InnerHtml = HtmlDocument.HtmlEncode(new_script);
和产生正确的脚本,在第二个例子说明,但<
和>
全部换成了<
和>
等
所以结果是:
<script type="text/JScript">
function thisForm_onsubmit(el)
{
var Checked = false
var Counter = 0
for (;Counter < el.choice.length; Counter++)
{
if (el.choice[Counter].checked)
{
Checked = true
el.action = el.choice[Counter].value
}
}
if (!Checked)
{
alert ("Please make a selection")
return false
}
}
</script>
我想到的使用InnerText而不是InnerHtml,这更有意义,因为我正在改变的不是真正的HTML,而是InnerText属性是只读的。
任何人都可以阐明为什么会发生这种情况,如果有解决方法吗?
你多线程工作,并在那里,这是获得共享的东西在这些线程之间?它看起来像是当多个线程都更新相同的值时会发生什么 - 您从一个输出的一部分输出与另一个输出的一部分输出混合。 –
不,没有多线程 – ElenaDBA
这是非常奇怪的,因为到目前为止我已经转换为许多文件的JavaScript没有问题,只是这个特定的一个给我一个问题 – ElenaDBA