- //======================
- //此事例中js代码虽被赋值给oDiv,But js is not executed.
- //======================
- <script type="text/javascript">
- var callback=function(){
- if (xmlHttp.readyState==4&&xmlHttp.status==200){
- $("#oDiv").innerHTML="<script type='text/javascript'>alert('Hello,World!');</script>";
- }
- }
- </script>
- //=================================
- //Usage execJS.js is solved for it,which is created by NetFriend
- //=================================
- <script src='execJS.js' type="text/javascript"></script>
- <Script language="javascript">
- var str="设置InnerHTML的脚本<br/>1:直接写在页面<script>alert(111)</"+"script><br/>2:通过src引入js文件<script
- type='text/javascript' src='hello.js'></"+"script>";
- window.onload=function(){
- var djs=document.getElementById('divJS');
- djs.innerHTML=str;//这里直接设置包含有script标签的内容时里面的js脚本无法运行
- //第一个参数是需要设置innerHTML的控件的id,第二个参数是需要设置的hmtl字符串,第3个参数是延时多少秒执行,这个可以省略如果省略时默认为10
- set_innerHTML('divJS',str);//需要使用innerhtml.js中的set_innerHTML来执行设置的script标签中的js代码
- }
- </script>
- //========================================
- //follow execjs.js
- //========================================
- /* innerhtml.js
- * Copyright Ma Bingyao <andot@ujn.edu.cn>
- * Version: 1.9
- * LastModified: 2006-06-04
- * This library is free. You can redistribute it and/or modify it.
- * http://www.coolcode.cn/?p=117
- */
-
以下是javascript的函数.
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[\s\S]*$/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(/src\s*=\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;
s += 2000;
}
else {
s += 10;
}
}
else {
script.text = scripts[i].text;
s += 10;
}
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)";
add_script += ".appendChild(global_script_pool['" + script.id + "']);\n";
window.setTimeout(add_script, s);
remove_script = "document.getElementsByTagName('head').item(0)";
remove_script += ".removeChild(document.getElementById('" + script.id + "'));\n";
remove_script += "delete global_script_pool['" + script.id + "'];\n";
window.setTimeout(remove_script, s + 10000);
}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);
}
调用方式为:
set_innerHTML('All_foot_Count',temp);