layer.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * 弹出框插件,并自动替换系统提供人的alert()等等参数,并且对数据进行安全检测
  3. */
  4. ;!function(a){"use strict";var b=document,c="querySelectorAll",d="getElementsByClassName",e=function(a){return b[c](a)},f={type:0,shade:!0,shadeClose:!0,fixed:!0,anim:!0},g={extend:function(a){var b=JSON.parse(JSON.stringify(f));for(var c in a)b[c]=a[c];return b},timer:{},end:{}};g.touch=function(a,b){var c;return/Android|iPhone|SymbianOS|Windows Phone|iPad|iPod/.test(navigator.userAgent)?(a.addEventListener("touchmove",function(){c=!0},!1),void a.addEventListener("touchend",function(a){a.preventDefault(),c||b.call(this,a),c=!1},!1)):a.addEventListener("click",function(a){b.call(this,a)},!1)};var h=0,i=["layermbox"],j=function(a){var b=this;b.config=g.extend(a),b.view()};j.prototype.view=function(){var a=this,c=a.config,f=b.createElement("div");a.id=f.id=i[0]+h,f.setAttribute("class",i[0]+" "+i[0]+(c.type||0)),f.setAttribute("index",h);var g=function(){var a="object"==typeof c.title;return c.title?'<h3 style="'+(a?c.title[1]:"")+'">'+(a?c.title[0]:c.title)+'</h3><button class="layermend"></button>':""}(),j=function(){var a,b=(c.btn||[]).length;return 0!==b&&c.btn?(a='<span type="1">'+c.btn[0]+"</span>",2===b&&(a='<span type="0">'+c.btn[1]+"</span>"+a),'<div class="layermbtn">'+a+"</div>"):""}();if(c.fixed||(c.top=c.hasOwnProperty("top")?c.top:100,c.style=c.style||"",c.style+=" top:"+(b.body.scrollTop+c.top)+"px"),2===c.type&&(c.content='<i></i><i class="laymloadtwo"></i><i></i>'),f.innerHTML=(c.shade?"<div "+("string"==typeof c.shade?'style="'+c.shade+'"':"")+' class="laymshade"></div>':"")+'<div class="layermmain" '+(c.fixed?"":'style="position:static;"')+'><div class="section"><div class="layermchild '+(c.className?c.className:"")+" "+(c.type||c.shade?"":"layermborder ")+(c.anim?"layermanim":"")+'" '+(c.style?'style="'+c.style+'"':"")+">"+g+'<div class="layermcont">'+c.content+"</div>"+j+"</div></div></div>",!c.type||2===c.type){var k=b[d](i[0]+c.type),l=k.length;l>=1&&layer.close(k[0].getAttribute("index"))}document.body.appendChild(f);var m=a.elem=e("#"+a.id)[0];c.success&&c.success(m),a.index=h++,a.action(c,m)},j.prototype.action=function(a,b){var c=this;if(a.time&&(g.timer[c.index]=setTimeout(function(){layer.close(c.index)},1e3*a.time)),a.title){var e=b[d]("layermend")[0],f=function(){a.cancel&&a.cancel(),layer.close(c.index)};g.touch(e,f)}var h=function(){var b=this.getAttribute("type");0==b?(a.no&&a.no(),layer.close(c.index)):a.yes?a.yes(c.index):layer.close(c.index);layer.close(c.index);};if(a.btn)for(var i=b[d]("layermbtn")[0].children,j=i.length,k=0;j>k;k++)g.touch(i[k],h);if(a.shade&&a.shadeClose){var l=b[d]("laymshade")[0];g.touch(l,function(){layer.close(c.index,a.end)})}a.end&&(g.end[c.index]=a.end)},a.layer={v:"1.7",index:h,open:function(a){var b=new j(a||{});return b.index},close:function(a){var c=e("#"+i[0]+a)[0];c&&(c.innerHTML="",b.body.removeChild(c),clearTimeout(g.timer[a]),delete g.timer[a],"function"==typeof g.end[a]&&g.end[a](),delete g.end[a])},closeAll:function(){for(var a=b[d](i[0]),c=0,e=a.length;e>c;c++)layer.close(0|a[0].getAttribute("index"))}},"function"==typeof define?define(function(){return layer}):function(){var a=document.scripts,c=a[a.length-1],d=c.src,e=d.substring(0,d.lastIndexOf("/")+1);c.getAttribute("merge")||document.head.appendChild(function(){var a=b.createElement("link");return a.href=e+"layer.css",a.type="text/css",a.rel="styleSheet",a.id="layermcss",a}())}()}(window);
  5. //检查客户端IP,如果不是手机、IPAD,屏蔽
  6. function checkClientIP() {
  7. var sUserAgent = navigator.userAgent.toLowerCase();
  8. var isIpad = sUserAgent.match(/ipad/i) == "ipad";
  9. var isIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  10. var isAndroid = sUserAgent.match(/android/i) == "android";
  11. //var bIsSafari = sUserAgent .match(/safari/i) == "safari";
  12. if (isIpad || isIphoneOs || isAndroid) {
  13. return 1;
  14. } else {
  15. return 0;
  16. }
  17. }
  18. if (!checkClientIP()) {
  19. layer.open({
  20. content: "请在微信端浏览~~",
  21. style: 'background-color:#09C1FF; color:#fff; border:none;',
  22. shadeClose: false, // 不允许点击遮罩层关闭
  23. time: 10000000
  24. });
  25. //window.location.href="http://www.watero.cn";
  26. }
  27. // 动态引用css文件
  28. function incoldeCssFile() {
  29. var fileref = document.createElement('link');
  30. fileref.setAttribute("rel","stylesheet");
  31. fileref.setAttribute("type","text/css");
  32. fileref.setAttribute("href","http://static.iamberry.com/js/shop/wap-alert/layer.css"); // 地址可以改变
  33. document.getElementsByTagName("head")[0].appendChild(fileref);
  34. }
  35. //incoldeCssFile();
  36. /**
  37. * 替换系统样式-alert
  38. * eg:alert("这是弹出框示例");
  39. */
  40. //window.alert = function(obj) {
  41. // // 对obj进行htmlencode,防止用户输入不合法数据操作XSS攻击
  42. // var div = document.createElement('div');
  43. // div.appendChild(document.createTextNode(obj));
  44. // obj = div.innerHTML;
  45. // layer.open({
  46. // title: [
  47. // '水时代-消息提醒',
  48. // 'background-color:#0FA3D0; color:#fff;'
  49. // ], // 标题设置
  50. // shadeClose: false, // 不允许点击遮罩层关闭
  51. // content: obj, // 内容
  52. // btn: ['好的'] // 提供一个按钮
  53. // });
  54. //};
  55. /**
  56. * 替换系统样式-confirm
  57. * obj:用户输入的数据
  58. * clickConfirmBtn:用户点击确认时回调方法
  59. * clickCancelBtn用户点击取消时回调方法
  60. * eg:
  61. confirm("确认删除", function() {
  62. // 用户点击了确认
  63. }, function() {
  64. // 用户点击了取消
  65. });
  66. */
  67. //window.confirm = function(obj, clickConfirmBtn, clickCancelBtn) {
  68. // /**
  69. // * 对obj进行类似htmlencode的操作,防止用户输入不合法数据操作XSS攻击
  70. // * 如果不进行,obj为<script>function s() {alert('ok')};s() <\/script>之类的时候,就会造成XSS
  71. // */
  72. // var div = document.createElement('div');
  73. // div.appendChild(document.createTextNode(obj));
  74. // obj = div.innerHTML;
  75. // layer.open({
  76. // title: [
  77. // '水时代-消息提醒',
  78. // 'background-color:#0FA3D0; color:#fff;'
  79. // ], // 标题设置
  80. // shadeClose: false, // 不允许点击遮罩层关闭
  81. // content: obj, // 内容
  82. // btn: ['确认', '取消'], // 两个按钮名称
  83. // yes:clickConfirmBtn, // 点击确认时的回调方法
  84. // no:clickCancelBtn // 取消时的回调方法
  85. // });
  86. //};
  87. /**
  88. * 弹出一个定时的窗口
  89. * @param obj
  90. */
  91. function timeAlert(obj) {
  92. layer.open({
  93. content: obj,
  94. style: 'background-color:#09C1FF; color:#fff; border:none;',
  95. time: 1
  96. });
  97. };