如果不清楚innerHtml的脚本如何运行的可以参考这篇文章http://www.jb51.net/article/234.htm下面的代码目的就是防止有些网站需要将一些特别的广告放到script中,大家可以完全用iframe替换,联盟广告大家可以用后加载的方式,特别处理。推荐的代码的写法: 复制代码 代码如下: if(PageClass==2) { //顶部通栏 468*60用iframe方式加载 set_innerHTML('Head_2_2',All_Top_468_60); //顶部468*60 set_innerHTML('Head_5',All_Top_970_60); Temp_Adv='src="/show/html/List_1_1_4.html" style="margin-bottom:5px;">'; set_innerHTML('List_1_1_4',Temp_Adv); //下载右边文字 直接地址+图片 推荐 Temp_Adv='| 脚本之家 '; document.getElementById("Soft_1_2_4_h2").innerHTML+=Temp_Adv; // set_innerHTML('Soft_1_2_2_1_2',Temp_Adv); } 上面的是4668*60 上面的是4668*602 因为直接用的是inner所有不显示。 var PageClass=1 //上面主要是为了区分页面,首页,列表页,内容页的控制,原理不错 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']);", 20); 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 + ");", 20); 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); } // JavaScript Document if( PageClass ==null) { var PageClass = 0; } // ==============公共变量===================== var delta=0.15; //==================函数列表========================= function PostCookie(cookieName) { var expdate = new Date(); expdate.setTime(expdate.getTime() + 604800000); document.cookie=cookieName+";expires="+expdate.toGMTString()+";path = /;domain=.jb51.net"; } //用于产生随机轮转广告 i 随机个数 function GetRandom(i) { var dt = new Date(); var hr = dt.getSeconds(); hr = hr%i; return hr; } //读取Cookies值 function getCookie(cookieName) { var cookieString =document.cookie; var start = cookieString.indexOf(cookieName + '='); // 加上等号的原因是避免在某些 Cookie 的值里有 // 与 cookieName 一样的字符串。 if (start == -1) // 找不到 return null; start += cookieName.length + 1; var end = cookieString.indexOf(';', start); if (end == -1) return unescape(cookieString.substring(start)); return unescape(cookieString.substring(start, end)); } //下载指定的文件 function PosDownload(url) { top.location.href =url; return true; } //复制标题和网址 function copypageurltitle() { var clipBoardContent=''; clipBoardContent =document.title; clipBoardContent+='\r\n'; clipBoardContent+=document.location; window.clipboardData.setData("Text",clipBoardContent); alert("标题和网址已经拷贝,请发给你QQ/Msn或者可以粘贴到论坛上和您的好友一起分享 ^_^ "); } var IsShowAds = true; //是否显示广告 if(getCookie("IsShowAds")=="False") IsShowAds=false; //========================================================================================= if(PageClass==1) { PostCookie("IsShowAds=False"); //上面的 cookies主要是为了控制,广告显示以后,刷新后或打开别的页面 广告就不显示。 //顶部通栏 468*60 Temp_Adv="<scri"+"pt src='http://img.jb51.net/imgby/logo_m.js'>"+"pt>"; set_innerHTML('jb51',Temp_Adv); Temp_Adv2="<scri"+"pt src='http://img.jb51.net/imgby/logo_r.js'>"+"pt>"; set_innerHTML('jb512',Temp_Adv2); //在jb51上面加一个广告1 Temp_Adv3= "<a href='http://www.jb51.net/' target='_blank' title='这是广告'> <img src='http://www.jb51.net/images/logo.gif' border='0' alt='这是广告'> "; document.getElementById("jb51").innerHTML +=Temp_Adv3; //在jb51上面加一个广告2 var aNode =document.createElement("div"); aNode.style.marginBottom =2; aNode.innerHTML = "<a href='http://www.jb51.net/' target='_blank' title='这是广告'> <img src='http://www.jb51.net/images/logo.gif' border='0' alt='这是广告'> "; var Prean=document.getElementById("ss"); Prean.parentNode.insertBefore(aNode,Prean); } [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 经测试发现,如果想在有运行script的上面加广告是不可能的因为这些脚本是后执行的,所有脚本广告可以直接在脚本中写入,不要随便的后加入了。 但如果是其它的广告是可以前面加入广告的,一些网站都是直接通过script控制全站的广告,虽然脚本之家暂时没有使用这种方法,不过在以后我们也会参考这个方法。 最主要的是广告,大家可以尽量的避免使用script,尽量的使用iframe或直接代码,图片链接等方法是非常不错的。