基于frame的弹出框
程序员文章站
2022-05-21 14:56:55
...
自己写了个基于frame的弹出框,解决了参数的传递,先记录下来,以便于以后自己下载!
效果如下:
效果如下:
/** * author: code by ShareMu */ //获得顶层窗口对象 var topWindow = (function(p, c) { while (p != c) { c = p p = p.parent } return c })(window.parent, window); //弹出窗口方法 function ShowModelDialog(url, titletext, backFn, width, height, params) { var msgw, msgh, bordercolor; msgw = (width ? width : 600);// 提示窗口的宽度 msgh = (height ? height : 400);// 提示窗口的高度 bordercolor = "#336699";// 提示窗口的边框颜色 titlecolor = "#99CCFF";// 提示窗口的标题颜色 var sWidth, sHeight; var thisW = topWindow; sWidth = thisW.document.documentElement.clientWidth; sHeight = thisW.document.documentElement.clientHeight; // 背景div var bgDivId = getRandomStr(16, true, true, true); var bgObj = new Element("div"); bgObj.setAttr("id", bgDivId); bgObj.addStyle("position", "absolute"); bgObj.addStyle("top", "0"); bgObj.addStyle("background", "#777"); bgObj.addStyle("opacity", "0.6"); bgObj.addStyle("left", "0"); bgObj.addStyle("width", "100%"); bgObj.addStyle("height", sHeight); bgObj = bgObj.element; thisW.document.body.appendChild(bgObj); var iframeObj = new Element("iframe"); var iframeObjId = getRandomStr(16, true, true, true); iframeObj.setAttr("id", iframeObjId); iframeObj.setAttr("name", "ifname"); iframeObj.addStyle("position", "absolute"); iframeObj.addStyle("top", "25px"); iframeObj.addStyle("filter", "Chroma(Color=white)"); iframeObj.addStyle("zIndex", "10000"); iframeObj.addStyle("width", "100%"); iframeObj.addStyle("left", "0"); iframeObj.addStyle("scrolling", "auto"); iframeObj.addStyle("height", msgh); if(url) { if(params) { if(url.indexof("?")!=-1) { url = url+"&"+params; }else{ url = url+"?"+params; } } iframeObj.setAttr("src", url); } iframeObj = iframeObj.element; iframeObj.style.border = "0"; iframeObj.style.background= "white"; // 消息div var msgDivId = getRandomStr(16, true, true, true); var msgObj = thisW.document.createElement("div") msgObj.setAttribute("id", msgDivId); msgObj.setAttribute("align", "center"); msgObj.style.position = "absolute"; msgObj.style.background = "white"; msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif"; msgObj.style.width = msgw + "px"; msgObj.style.height = (msgh + 24) + "px"; var toph=sHeight - msgh; if(toph<0) { toph = 0; } msgObj.style.top = (thisW.document.documentElement.scrollTop + toph/ 2) + "px"; msgObj.style.left = (sWidth - msgw) / 2 + "px"; msgObj.style.zIndex = "1000"; // 消息标题 var msgTitleId = getRandomStr(16, true, true, true); var titleTable = thisW.document.createElement("div"); //var titleTableTR = thisW.document.createElement("div"); var titleTableMsgTD = thisW.document.createElement("div"); var closeTD = thisW.document.createElement("div"); titleTable.style.zIndex = "1000"; titleTable.id = msgTitleId; titleTable.style.width = msgw-5; titleTableMsgTD.style.width = msgw-2; closeTD.style.right = "5"; closeTD.style.top = "0"; titleTable.style.display="inline"; titleTable.style.border = "1px solid " + titlecolor; //titleTable.style.cursor = "pointer"; closeTD.style.position = "absolute"; titleTableMsgTD.setAttribute("align", "left"); titleTableMsgTD.style.height = "24"; titleTableMsgTD.style.border = "1px solid " + titlecolor; titleTable.setAttribute("align", "left"); closeTD.setAttribute("align", "right"); titleTable.style.background = bordercolor; titleTable.style.color = "white"; var msgtitleTableIdl = getRandomStr(16, true, true, true); titleTableMsgTD.innerHTML = (titletext?titletext:"弹出框"); titleTableMsgTD.style.margin = "0"; //titleTableMsgTD.innerHTML = "弹出框"; closeTD.style.cursor = "pointer"; closeTD.innerHTML = "关闭"; closeTD.onclick = function() { var rvalue = thisW.frames[iframeObjId].returnValue; if (backFn) { backFn(rvalue); } thisW.document.body.removeChild(bgObj); thisW.document.getElementById(msgDivId).removeChild(titleTable); thisW.document.body.removeChild(msgObj); } closeTD.style.margin = "0"; titleTable.appendChild(titleTableMsgTD); titleTable.appendChild(closeTD); msgObj.appendChild(titleTable); msgObj.appendChild(iframeObj); thisW.document.body.appendChild(msgObj); //alert(thisW.document.getElementById(msgTitleId).id); } function Element(type, paret) { this.element = topWindow.document.createElement(type); } Element.prototype.setAttr = function(name, value) { this.element.setAttribute(name, value); } Element.prototype.addStyle = function(name, value) { eval("this.element.style." + name + "=\"" + value + "\";"); } //获得随机字符串 元素ID function getRandomStr(str_0, str_1, str_2, str_3) { var seed_array = new Array(); var seedary; var i; seed_array[0] = "" seed_array[1] = "a b c d e f g h i j k l m n o p q r s t u v w x y z"; seed_array[2] = "a b c d e f g h i j k l m n o p q r s t u v w x y z"; seed_array[3] = "0 1 2 3 4 5 6 7 8 9"; if (!str_1 && !str_2 && !str_3) { str_1 = true; str_2 = true; str_3 = true; } if (str_1) { seed_array[0] += seed_array[1]; } if (str_2) { seed_array[0] += " " + seed_array[2]; } if (str_3) { seed_array[0] += " " + seed_array[3]; } seed_array[0] = seed_array[0].split(" "); seedary = "" for (i = 0; i < str_0; i++) { seedary += seed_array[0][Math.round(Math.random() * (seed_array[0].length - 1))] } return (seedary); }