开发一个系统的过程中可能会碰到需要在浏览器端动态输出 javascript脚本的情况,前几天遇到一个怪事. 代码在IE7环境下 一切正常,一遇到IE6的浏览器就出错.找了很久才找到问题的根本原因,是动态输出的脚本中 包含有 "</script> "的脚本内容在IE6中被当成了 脚本结束的标记.
代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
for (var i = 0; i < E8MenuConfig.Items.length; i++)
{
if (E8MenuConfig.Items[i][1] == MenuId)
{
MenuUrl = E8MenuConfig.Items[i][3];
if(blnHasAdd == false)
{
strTemp += "<script>var blnSubShow = false;";
strTemp += "function HidPopup(){if(blnSubShow==false){if(parent.blnSubShow!=null) parent.blnSubShow=false;if(" + parentTreeNode + ".E8MenuConfig.WindowsList[" + deep + "]!=null)" + parentTreeNode + ".E8MenuConfig.WindowsList[" + deep + "].hide();}}";
strTemp += "</script>";
strTemp += "<body leftmargin=\"0\" topmargin=\"0\" scroll=\"no\" style=\"border:solid menu 0pxpx;\" onmouseover=\"clearTimeout(parent.popt);if(parent.blnSubShow!=null) parent.blnSubShow=true;\" onmouseout=\"parent.popt=setTimeout('HidPopup()',10);\" >\n";
strTemp += "<table width=\"100%\" height=\"100%\" style=\"border-collapse:collapse;\">\n";
}
blnHasAdd = true;
LineCount++;
if (E8MenuConfig.Items[i][2].length > MaxCharCount) MaxCharCount = E8MenuConfig.Items[i][2].length;
if (E8MenuConfig.Items[i][5]) //有下级菜单
{
strTemp += "<tr><td bgcolor=\"#C2D2E5\" style=\"border-top:#cccccc 1px solid;border-bottom:#666666 1px solid;border-left:#cccccc 1px solid;border-right:#666666 1px solid;mouse:hand; font-size:12px; color:#000000;text-align:left;vertical-align:center;CURSOR:hand\" onmouseover=\"this.bgColor='#dfdfdf';" + parentTreeNode + ".E8MenuConfig.ShowSubMenu('" + E8MenuConfig.Items[i][0] + "',this,''," + (deep+1) +"," + topL + ");\" onmouseout=\"this.blnSubShow=false;this.bgColor='#C2D2E5';\" height=\"20\" onclick=\"" + parentTreeNode + ".E8MenuConfig.GoToUrl('" + MenuUrl + "');\">" + E8MenuConfig.Items[i][2] + ">></td></tr>";
MaxCharCount += 2; //多了 两个 >符号
}
else
{
strTemp += "<tr><td bgcolor=\"#C2D2E5\" style=\"border-top:#cccccc 1px solid;border-bottom:#666666 1px solid;border-left:#cccccc 1px solid;border-right:#666666 1px solid;mouse:hand; font-size:12px; color:#000000;text-align:left;vertical-align:center;CURSOR:hand\" onmouseover=\"this.bgColor='#dfdfdf';\" onmouseout=\"this.blnSubShow=false;this.bgColor='#C2D2E5';\" height=\"20\" onclick=\"" + parentTreeNode + ".E8MenuConfig.GoToUrl('" + MenuUrl + "');\">" + E8MenuConfig.Items[i][2] + "</td></tr>";
}
}
}
if(blnHasAdd == true)
{
strTemp += "</table>\n";
strTemp += "</body>\n";
}
oPopup.document.body.innerHTML="";
if (strTemp.length > 0)
oPopup.document.write(strTemp);
由于此代码是在 浏览器端动态输出的,因此 在IE7以下版本中, 把这句代码 : strTemp += "</script>"; 中当作了脚本结束标记,产生错误.
解决方法为 ,把此代码 改为 strTemp += "</ " + "script>"; 即可
以后遇到动态输出脚本, 遇到标记语言,最好分开写.