JavaScript asynchronous return Data code introduction

Source: Internet
Author: User

Cases

The code is as follows Copy Code

var a= (function () {

var obj=null;

return {

Win:function (config) {
if (config && typeof config = = "Object") {
This.loadscriptfile (_tc.baseurl,
function () {


obj= $.dialog (config);

A.BFN ();

});
}

},

Bfn:fucntion () {

//.. Working with obj

}

};

})();


Here's an example of the data returned asynchronously:

<div id= "Test" >...</div>

<script type= "Text/javascript" >
Alert (' Test ');
</script>
<div id= "Test2" ... </div>

<script type= "Text/javascript" >
Alert (' Test2 ');
</script>

Code for the asynchronous operation (in jquery code, for example):

$.get (' test.php ', function (data) {
$ (' #container '). HTML (data);
})
In this case, the data in the asynchronous will be written to the container container, but the script in the two scripts will not run, and here's a solution:
1. Split the returned data into a script label
2. Adds text containing DOM elements to the specified container, adding the script between the text to the header to run (note: During the addition, the segments need to be run late because the script takes time to run, and the delay takes the longest time in the script)
3. Move the script added to the header after the script is finished
The following is a section of the online implementation code:

The code is as follows Copy Code
var global_html_pool = [];
var global_script_pool = [];
var global_script_src_pool = [];
var global_lock_pool = [];
var innerhtml_lock = null;
var document_buffer = "";

function set_innerhtml (obj_id, HTML, time) {
if (Innerhtml_lock = = null) {
Innerhtml_lock = obj_id;
}
else if (typeof (Time) = "undefined") {
global_lock_pool[obj_id + "_html"] = html;
Window.settimeout ("set_innerhtml (' + obj_id +" ', global_lock_pool[' "+ obj_id +" _html ']); ", 10;
Return
}
else if (Innerhtml_lock!= obj_id) {
global_lock_pool[obj_id + "_html"] = html;
Window.settimeout ("set_innerhtml" + obj_id + "', global_lock_pool['" + obj_id + "_html ')," + Time + ");", 10;
Return
}

function get_script_id () {
Return "Script_" + (new Date ()). GetTime (). ToString (36)
+ Math.floor (math.random () * 100000000). toString (36);
}

Document_buffer = "";

document.write = function (str) {
Document_buffer + + str;
}
Document.writeln = function (str) {
Document_buffer + = str + "n";
}

Global_html_pool = [];

var scripts = [];
html = html.split (/</script>/i);
for (var i = 0; i < html.length; i++) {
Global_html_pool[i] = Html[i].replace (/<script[ss]*$/ig, "");
Scripts[i] = {text: ', src: '};
Scripts[i].text = Html[i].substr (global_html_pool[i].length);
SCRIPTS[I].SRC = scripts[i].text.substr (0, Scripts[i].text.indexof (' > ') + 1);
SCRIPTS[I].SRC = Scripts[i].src.match (/srcs*=s* ("[^"]*) "|" ([^']*)'| ([^s]*) [S>]) /i);
if (SCRIPTS[I].SRC) {
if (Scripts[i].src[2]) {
SCRIPTS[I].SRC = scripts[i].src[2];
}
else if (Scripts[i].src[3]) {
SCRIPTS[I].SRC = scripts[i].src[3];
}
else if (Scripts[i].src[4]) {
SCRIPTS[I].SRC = scripts[i].src[4];
}
else {
SCRIPTS[I].SRC = "";
}
Scripts[i].text = "";
}
else {
SCRIPTS[I].SRC = "";
Scripts[i].text = Scripts[i].text.substr (Scripts[i].text.indexof (' > ') + 1);
Scripts[i].text = Scripts[i].text.replace (/^s*<!--s*/g, "");
}
}

var s;
if (typeof (Time) = "undefined") {
s = 0;
}
else {
s = time;
}

var script, Add_script, Remove_script;
for (var i = 0; i < scripts.length; i++) {
var add_html = "Document_buffer + + global_html_pool[" + i + "];n";
Add_html + + document.getElementById (' + obj_id + '). InnerHTML = Document_buffer;n ";
Script = document.createelement ("script");
if (SCRIPTS[I].SRC) {
SCRIPT.SRC = SCRIPTS[I].SRC;
if (typeof (Global_script_src_pool[script.src]) = = "undefined") {
GLOBAL_SCRIPT_SRC_POOL[SCRIPT.SRC] = true;
}
}
else {
Script.text = Scripts[i].text;
}
s + + 500;
Script.defer = true;
Script.type = "Text/javascript";
Script.id = get_script_id ();
Global_script_pool[script.id] = script;
Add_script = add_html;
Add_script + + document.getelementsbytagname (' head '). Item (0). appendchild (global_script_pool[' "+ script.id +" ']); n " ;
Window.settimeout (Add_script, s);
Remove_script = "document.getElementsByTagName" (' Head '). Item (0). RemoveChild (document.getElementById (' "+ script.id + "')); n";
Remove_script + = "Delete global_script_pool['" + script.id + "'];n";
Window.settimeout (Remove_script, S + 20000);
}

var end_script = ' If Document_buffer.match (/<\/script>/i)) {n ";
End_script + = "set_innerhtml (' + obj_id +" ', Document_buffer, "+ S +"); n ";
End_script + = "}n";
End_script + = "else {n";
End_script + + document.getElementById (' + obj_id + '). InnerHTML = Document_buffer;n ";
End_script + = "Innerhtml_lock = Null;n";
End_script + = "}";
Window.settimeout (End_script, s);
}
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.