_javascript techniques for dynamically generating IFRAME in JavaScript compatible with mainstream browsers

Source: Internet
Author: User
Tags visibility

The following code runs through under IE8 and has an error in IE9:

Copy Code code as follows:

Document.createelement (' <iframe id= "Yige-org-iframe" src= "Yige.org.logo.gif" style= "position:absolute;top:0; Left:0;width:1px;height:1px;visibility:hidden; " ></iframe> ');

Error tip: Exception:SCRIPT5022:DOM Exception:invalid_character_err (5)

Thinking Analysis:
First step: Compatible with Ie9,firefox,opera,safari and other browsers;

Copy Code code as follows:
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;");


Step Two: Compatible ie6-8: Because ie6-8 cannot modify the IFRAME's Name property

Copy Code code as follows:

var oframe = Isie? Document.createelement ("<iframe name=/" "+ This._framename +"/">"): Document.createelement ("iframe");
Oframe.name = "Iframname";

Comprehensive Solution:

Copy Code code as follows:

var Isie = (document.all)? True:false; Here is simply to determine whether it is ie, detailed browser judgment: Please refer to browser type detection
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"); o Frame.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 = ' Internal error, cannot display this content ';
}
}
}

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 (' <iframe name= ' + Ajaxframeid + ' "id=" ' + Ajaxframeid + ' "></iframe> ');
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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.