(function(){ var getChange=document.getElementById("dingyue");   getChange.onclick=function(){     confirmMsg("", function(){});   } var head = document.getElementsByTagName('head')[0]; var link = document.createElement( 'link' ); link.href = 'https://www.bughunter.cn/assets/css/dialog.css'; link.rel ='stylesheet' ; link.type = 'text/css' ; head.appendChild(link); //提供操作dom节点的统一方法 var domOperate = {}; domOperate = { //创建element对象, create: function(ele, cns){ var eleObj = document.createElement(ele); if(cns){ this.addClass(eleObj, cns); } return eleObj; }, //添加class,可以添加多个class,以空格为分隔符 addClass: function(ele, cn){ if(ele.className){ ele.className += ' ' + cn; }else{ ele.className += cn; } }, //移除class,可以移除多个class,以空格为分隔符 removeClass: function(ele, cn){ var cns = cn.split(' '); for(var i in cns){ if(cns[i] && this.hasClass(ele, cns[i])){ ele.className = ele.className.replace( new RegExp( "(\\s|^)" + cns[i] + "(\\s|$)" )," " ); } } ele.className = ele.className.replace(/^\s(.*)\s$/, '$1'); }, //是否包含某个class hasClass: function(ele, cn){ return !!ele.className.match( new RegExp( "(\\s|^)" + cn + "(\\s|$)") ); }, //添加css,style是一个对象,对象中的字段名是样式名,字段值是样式值 addCss: function(ele, style){ for(var i in style){ ele.style[i] = style[i]; } } } //事件处理 var EventUtil = { //绑定事件 addHandler: function(element, type, handler){ if(element.addEventListener){ element.addEventListener(type, handler, false); }else if(element.attachEvent){ element.attachEvent("on"+type, handler); }else{ element["on"+type] = handler; } } } //更改函数内部的this指向 Function.prototype.bind = function(context){ var self = this; return function () { return self.apply(context, arguments); } } //提交数据 function scribe(){ var email=document.getElementById("email-bg-1123").value; if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1){ var xmlhttp; if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else{// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","https://www.bughunter.cn/dysave.htm?email_bg="+document.getElementById("email-bg-1123").value+"&c=cGJjeHRMRjBiN1RXYXU1WGwvR1dIdz09",true); xmlhttp.onreadystatechange = function () { //绑定响应状态事件监听函数 if (xmlhttp.readyState == 4) { //监听readyState状态 if (xmlhttp.status == 200 || xmlhttp.status == 0) { //监听HTTP状态码 var info = xmlhttp.responseText; var o = eval("(" + info + ")"); //调用eval()把字符串转换为本地脚本 if(o.status==200){ alertMsg("订阅成功,请登录您的邮箱确认。", function(){}); } if(o.status==302){ alertMsg("您已订阅,请勿重复订阅。", function(){}); } if(o.status==303){ alertMsg("请勿重复频繁订阅。", function(){}); } } } } xmlhttp.send(); }else{ alert("邮箱格式不正确"); } } //var setting = {}; //对话框 function Dialog(){ this.mask= '';//遮罩层 this.title= '';//弹框标题 this.content= '';//弹框提示内容 this.buttonClose= '';//弹框关闭按钮 this.buttonOk= '';//弹框确定按钮 this.buttonOkAction='0'; this.buttonOkEvent= '';//弹框确定按钮绑定的事件 this.width= 300;//弹框的初始宽度 this.height= 200;//弹框的初始高度 this.screenWid= window.innerWidth;//浏览器文档部分的宽度 this.screenHei= window.innerHeight;//浏览器文档部分的高度 //初始化弹框html this.init= function(){ //setting = extend(setting, option); this.mask = domOperate.create('div', 'dialog-mask-bg_'); domOperate.addCss(this.mask, {'transition': 'all 0.5s'}); this.dialogContainer = domOperate.create('div', 'dialog-container-bg_'); //对话框头部 this.dialogHead = domOperate.create('div', 'dialog-head-bg_'); this.title = domOperate.create('p'); this.title.innerHTML = '订阅'; this.buttonClose = domOperate.create('span', 'btn-close'); this.buttonClose.innerHTML = 'X'; this.dialogHead.appendChild(this.title); // this.dialogHead.appendChild(this.buttonClose); this.dialogContainer.appendChild(this.dialogHead); //对话框内容 this.dialogContent = domOperate.create('div', 'dialog-content-bg_'); this.content = domOperate.create('p'); this.dialogContent.appendChild(this.content); this.dialogContainer.appendChild(this.dialogContent); //对话框底部 this.dialogFoot = domOperate.create('div', 'dialog-foot-bg_'); this.buttonOk = domOperate.create('button', 'btn-ok'); this.buttonOk.innerHTML = '确定'; this.dialogFoot.appendChild(this.buttonOk); this.dialogContainer.appendChild(this.dialogFoot); var body = document.getElementsByTagName('body')[0]; this.mask.style.visibility = 'hidden'; this.dialogContainer.style.visibility = 'hidden'; body.appendChild(this.mask); body.appendChild(this.dialogContainer); domOperate.addCss(this.dialogContainer, { 'left': (this.screenWid - this.width) / 2 + 'px', 'top': '0px', 'opacity': 1, 'transition': 'all 0.3s' }); }; //根据弹框内容重置弹框大小 this.setSize= function(){ domOperate.addCss(this.mask, { 'height': document.body.scrollHeight + 'px'}); var contentHei = this.content.offsetHeight; //123是对话框中间部分的高度,77是头部和 if(contentHei>123){ this.dialogContainer.style.height = (contentHei + 77) + "px"; } }; //给弹框上的按钮绑定响应事件 this.attachEvent= function(){ //var dialogObj = this;//取到当前弹框对象,在click响应事件中无法使用this得到当前弹框对象 /*EventUtil.addHandler(this.buttonClose, 'click', function () { dialogObj.hide(); }); EventUtil.addHandler(this.buttonOk, 'click', function(){ if(typeof dialogObj.buttonOkEvent == 'function'){ dialogObj.buttonOkEvent(); } dialogObj.hide(); });*/ //绑定事件响应函数的时候更改函数内部的this指向 EventUtil.addHandler(this.buttonClose, 'click', function () { this.hide(); }.bind(this)); EventUtil.addHandler(this.buttonOk, 'click', function(){ if(this.buttonOkAction=="1"){ scribe(); } if(typeof this.buttonOkEvent == 'function'){ this.buttonOkEvent(); } this.hide(); }.bind(this)); }; //弹框显示方法 this.show= function(msg, callback){ if(msg==""){ this.buttonOkAction='1'; this.content.innerHTML = ' '; }else{ this.buttonOkAction='0'; this.content.innerHTML = msg; } this.buttonOkEvent = callback; this.setSize(); domOperate.addCss(this.mask, {'opacity': 0.5, 'visibility': 'visible'}); domOperate.addCss(this.dialogContainer, { 'left': (this.screenWid - this.width) / 2 + 'px', 'top': '40px', 'opacity': 1, 'visibility': 'visible' }); //domOperate.addClass(this.dialogContainer, 'elastic-bg_'); }; //弹框隐藏方法 this.hide= function(){ domOperate.addCss(this.mask, {'opacity': 0, 'visibility': 'hidden'}); domOperate.addCss(this.dialogContainer, { 'top': "0", 'opacity': 0, 'visibility': 'hidden'}); //domOperate.removeClass(this.dialogContainer, 'elastic-bg_'); } } //alert弹框,继承于Dialog Dialog.prototype.alertDialog = function () { this.init(); this.attachEvent(); } //confirm弹框,继承于Dialog,有自己独立的方法 Dialog.prototype.confirmDialog = function () { this.buttonCancel= '';//弹框取消按钮 this.initCancel= function () { this.buttonCancel = domOperate.create('button', 'btn-cancel'); this.buttonCancel.innerHTML = '取消'; this.dialogFoot.appendChild(this.buttonCancel); //var dialogObj = this;//取到当前弹框对象 EventUtil.addHandler(this.buttonCancel, 'click', function () { this.hide(); }.bind(this)); } this.init(); this.initCancel(); this.attachEvent(); } //alert对话框 var dialogA = new Dialog(); dialogA.alertDialog(); window.alertMsg = function(msg, callback){ dialogA.show(msg, callback); } //confirm对话框 var dialogC = new Dialog(); dialogC.confirmDialog(); window.confirmMsg = function (msg, callback) { dialogC.show(msg, callback); } //当浏览器大小变化时,重新计算弹框的位置 window.onresize = function () { dialogA.screenWid = window.innerWidth; domOperate.addCss(dialogA.dialogContainer, { 'left': (dialogA.screenWid - dialogA.width) / 2 + 'px' }); dialogC.screenWid = window.innerWidth; domOperate.addCss(dialogC.dialogContainer, { 'left': (dialogC.screenWid - dialogC.width) / 2 + 'px' }); } function extend(des, src, override){ if(src instanceof Array){ for(var i = 0, len = src.length; i < len; i++){ extend(des, src[i], override); } } for( var i in src){ if(override || !(i in des)){ des[i] = src[i]; } } return des; } })();