js兼容不通瀏覽器問題

以下代碼在IE8下運行通過,在IE9中出錯:
document.createElement('<iframe id="yui-history-iframe" src="http://images.cnblogs.com/defaults/transparent-pixel.gif" style="position:absolute;top:0;left:0;width:1px;height:1px;visibility:hidden;"></iframe>');
錯誤提示: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", "http://images.cnblogs.com/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("<iframe name=/"" + this._FrameName + "/">") : 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("<iframe name=/"" + this._FrameName + "/">") : document.createElement("iframe");
oFrame.name = "iframName";
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章