Javascript is compatible with the iframe method dynamically generated by mainstream browsers.

Source: Internet
Author: User

The following code passes in IE8 and an error occurs in IE9:Copy codeThe Code is 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 message: exception: SCRIPT5022: DOM Exception: INVALID_CHARACTER_ERR (5)

Train of Thought Analysis:
Step 1: compatible with IE9, firefox, Opera, Safari, and other browsers;

Copy codeThe Code is 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 2: compatible with IE6-8: Since the ie6-8 cannot modify the name attribute of iframe
Copy codeThe Code is as follows:
Var oFrame = isIE? Document. createElement ("<iframe name =/" "+ this. _ FrameName +"/">"): document. createElement ("iframe ");
OFrame. name = "iframName ";

Comprehensive Solution:
Copy codeThe Code is as follows:
Var isIE = (document. all )? True: false; // you can simply judge whether the browser is IE. For more information, see 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"); 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 = condition (formid).tar get;

Var handleResult = function (){
Var s = '';
Var evaled = false;

Showloading ('None ');
Try {
S = cipher (ajaxframeid=.content=doc ument. XMLDocument. text;
} Catch (e ){
Try {
S = certificate (ajaxframeid=.content?#document.doc umentElement. firstChild. wholeText;
} Catch (e ){
Try {
S = certificate (ajaxframeid=.content?#document.doc umentElement. firstChild. nodeValue;
} Catch (e ){
S = 'internal error, unable to 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) condition (formid).tar get = 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 ();
Authorization (formidapps.tar get = ajaxframeid;
$ (Formid). action + = '& inajax = 1 ';
$ (Formid). submit ();
Return false;
}

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.