以下代码在ie8下运行通过,在ie9中出错:
复制代码 代码如下:
document.createelement('');
错误提示:exception : script5022: dom exception: invalid_character_err (5)思路分析:
第一步:兼容ie9,firefox,opera,safari等浏览器;
复制代码 代码如下:
var iframe = document.createelement(iframe);
iframe.setattribute(id, yui-history-iframe);
iframe.setattribute(src, ../../images/defaults/transparent-pixel.gif);
iframe.setattribute(style,position:absolute;top:0;left:0;width:1px;height:1px;visibility:hidden;);
第二步:兼容ie6-8:由于ie6-8 不能修改iframe的name属性
复制代码 代码如下:
var oframe = isie ? document.createelement() : document.createelement(iframe);
oframe.name = iframname;
综合解决办法:
复制代码 代码如下:
var isie = (document.all) ? true: false; //这里仅仅简单的对是否是ie进行判断,详细浏览器判断:请参考浏览器类型侦测
var ua = navigator.useragent.tolowercase().match(/msie ([/d.] + ) / )[1];
if (ua == 9.0) {
isie = false;
}
var oframe = isie ? document.createelement() : document.createelement(iframe);oframe.name = iframname;
//=========================function ajaxpost(formid, showid, waitid, showidclass, submitbtn, recall) {
var waitid = typeof waitid == 'undefined' || waitid === null ? showid : (waitid !== '' ? waitid : '');
var showidclass = !showidclass ? '' : showidclass;
var ajaxframeid = 'ajaxframe';
var ajaxframe = $(ajaxframeid);
var formtarget = $(formid).target;
var handleresult = function() {
var s = '';
var evaled = false;
showloading('none');
try {
s = $(ajaxframeid).contentwindow.document.xmldocument.text;
} catch (e) {
try {
s = $(ajaxframeid).contentwindow.document.documentelement.firstchild.wholetext;
} catch (e) {
try {
s = $(ajaxframeid).contentwindow.document.documentelement.firstchild.nodevalue;
} catch (e) {
s = '内部错误,无法显示此内容';
}
}
}
if (s != '' && s.indexof('ajaxerror') != -1) {
evalscript(s);
evaled = true;
}
if (showidclass) {
$(showid).classname = showidclass;
if (submitbtn) {
submitbtn.disabled = false;
}
}
if (!evaled && (typeof ajaxerror == 'undefined' || !ajaxerror)) {
ajaxinnerhtml($(showid), s);
}
ajaxerror = null;
if ($(formid)) $(formid).target = formtarget;
if (typeof recall == 'function') {
recall();
} else {
eval(recall);
}
if (!evaled) evalscript(s);
ajaxframe.loading = 0;
$('append_parent').removechild(ajaxframe);
};
if (!ajaxframe) {
try {
ajaxframe = document.createelement('');
} catch (e) {
ajaxframe = document.createelement('iframe');
ajaxframe.name = ajaxframeid;
ajaxframe.id = ajaxframeid;
}
ajaxframe.style.display = 'none';
ajaxframe.loading = 1;
$('append_parent').appendchild(ajaxframe);
} else if (ajaxframe.loading) {
return false;
}
_attachevent(ajaxframe, 'load', handleresult);
showloading();
$(formid).target = ajaxframeid;
$(formid).action += '&inajax=1';
$(formid).submit();
return false;
}