JS json随机广告 前端优化

注意:[ ]符号在JavaScript中定义一个数组,{ }则定义一个对象

javascript随机数生成方法

1.使用内置的随机数发生方法:
Math.random(); //该方法产生一个0到1之间的浮点数。
Math.floor(Math.random()*10+1); //1-10
Math.floor(Math.random()*24);//0-23 

2.基于时间,亦可以产生随机数:
var now=new Date(); 
var number = now.getSeconds(); //这将产生一个基于目前时间的0到59的整数。 
var now=new Date(); 
var number = now.getSeconds()%43; //这将产生一个基于目前时间的0到42的整数。

 

创建随机图片,这是利用随机数所能做的最简单的事了。此例中,我们假设有10张图片,  banner1.gif.....banner10.gif,每当文档被载入时,我们希望能随机地显示这些图片。

Java代码   收藏代码
  1. <script language="JavaScript"><!--    
  2. document.write('<img src="../images/banner' + rand(10) + '.gif" width=400 height=40>');    
  3. //--></script>    

4.JS生成不重复的随机数组

Java代码   收藏代码
  1. <script type="text/javascript">  
  2.     function getkfArrayItems(s, e, num) {  
  3.         var arr = new Array();  
  4.         for (i = s; i < e; i++) {  
  5.             arr[i] = i;  
  6.         }  
  7.         var temp_array = new Array();  
  8.         for (var index in arr) {  
  9.             temp_array.push(arr[index]);  
  10.         }  
  11.         var return_array = new Array();  
  12.         for (var i = 0; i < num; i++) {  
  13.             if (temp_array.length > 0) {  
  14.                 var arrIndex = Math.floor(Math.random() * temp_array.length);  
  15.                 return_array[i] = temp_array[arrIndex];  
  16.                 temp_array.splice(arrIndex, 1);//入栈后删除 即去重  
  17.             } else {  
  18.                 break;  
  19.             }  
  20.         }  
  21.         return return_array;  
  22.     }  
  23.     //得到数组的随机元素12个  
  24.     kf_array = getkfArrayItems(02012);  
  25. </script>  

广告所有列表ads.js

Java代码   收藏代码
  1. <!--  
  2. document.write("<div class=\"gimg_li\">\r\n    <a href=\"/goto.html?url=http://code.116b.com/htmlcode/2499.html\" target=\"_blank\" title=\"_blank\">\r\n     <img src=\"http://img.265g.com/images/gg/yxwz0926.jpg\" width=\"50\" height=\"50\" />英雄王座</a></div>\r\n<div class=\"gimg_li\">\r\n    <a href=\"/goto.html?url=http://tongji.wan5d.com/pubpage/13_20101209829.html \"\r\n        target=\"_blank\">\r\n        <img src=\"http://img.265g.com/images/gg/wd09221.jpg\" width=\"50\" height=\"50\" />御宠风云</a></div>\r\n<div class=\"gimg_li\">\r\n    <a href=\"/goto.html?url=http://game.51.com/act/51wt/html?from=265g_1\" target=\"_blank\">\r\n        <img src=\"http://img.265g.com/images/gg/5109281.jpg\" width=\"50\" height=\"50\" />神仙道</a></div>\r\n<div class=\"gimg_li\">\r\n    <a href=\"/goto.html?url=http://game.51.com/act/51wt/html?from=265g_2\" target=\"_blank\">\r\n        <img src=\"http://img.265g.com/images/gg/smzt09281.jpg\" width=\"50\" height=\"50\" />神魔遮天</a></div>\r\n<div class=\"gimg_li\">\r\n    <a href=\"/goto.html?url=http://lm.189play.com/?zj12=265\" \r\n        target=\"_blank\" title=\"战将OL\">\r\n        <img src=\"http://img.265g.com/images/gg/zj0803.jpg\" width=\"50\" height=\"50\" />战将OL</a></div>\r\n<div class=\"gimg_li\">\r\n    <a href=\"/goto.html?url=http://tc.zhaizu.com/shanhai/flash4/index.php?id=10240\" target=\"_blank\">\r\n        <img src=\"http://img.265g.com/images/gg/zz09011.jpg\" width=\"50\" height=\"50\" />山海创世录</a></div>\r\n<div class=\"gimg_li\">\r\n    <a href=\"/goto.html?url=http://un.3737.com/count/index.php?webid=17&adid=667\"\r\n        target=\"_blank\">\r\n        <img src=\"http://img.265g.com/images/gg/37370801.jpg\" width=\"50\" height=\"50\" />西游征途</a></div>\r\n<div class=\"gimg_li\">\r\n    <a href=\"/goto.html?url=http://gg.xiyou6.com/265g.html?sid=10\" target=\"_blank\">\r\n        <img src=\"http://img.265g.com/images/gg/dp0823.jpg\" width=\"50\" height=\"50\" />极品修真</a></div>\r\n<div class=\"gimg_li\">\r\n    <a href=\"/goto.html?url=http://tg.139p.com/zhfy/zh_265g.html?p=s&s=[plid]\" target=\"_blank\">\r\n        <img src=\"http://img.265g.com/images/gg/zhfy0923.jpg\" width=\"50\" height=\"50\" />战魂风云</a></div>\r\n<div class=\"gimg_li\">\r\n    <a href=\"/goto.html?url=http://img.kd008.com/gg/10147.html\" target=\"_blank\">\r\n<img src=\"http://img.265g.com/images/gg/xdzz0923.jpg\" width=\"50\" height=\"50\" />现代战争2\r\n</a\r\n></div>\r\n<div class=\"gimg_li\">\r\n    <a href=\"/goto.html?url=http://gg.9c.com/gg/track/event?id=18.54.1&cd=ggcc&sn=33948879f52cfd4f41a4b125bdfe1da9e3154092&url=http%3A%2F%2Fimages.9c.com%2Fwztx%2F1%2F2%2F265g%2F265g_kf%2F265g_kf.html\" target=\"_blank\">\r\n        <img src=\"http://img.265g.com/images/gg/wztx09261.jpg\" width=\"50\" height=\"50\" />王者之战</a></div>\r\n<div class=\"gimg_li\">\r\n    <a href=\"/goto.html?url=http://p.17kuxun.com/s/1/246/126.html\" target=\"_blank\">\r\n        <img src=\"http://img.265g.com/images/gg/sssg0826.jpg\" width=\"50\" height=\"50\" />盛世三国</a></div>");  
  3. -->  

随机广告列表

Java代码   收藏代码
  1. <script src="jquery.js" type="text/javascript"></script>  
  2. <div class="top_kaifu"></div>  
  3. <div id="div_zxkf_h" style="display: none">  
  4.     <script src="ads.js" type="text/javascript"></script>  
  5. </div>  
  6. <script type="text/javascript">  
  7.     var kfhtml='';  
  8.     for (i = 0; i < kf_array.length; i++)  
  9.     {  
  10.         kfhtml+='<div class="gimg_li">'+$('#div_zxkf_h>div').eq(kf_array[i]).html()+'</div>';  
  11.     }  
  12.     $('.top_kaifu').append('<i>最新开服</i>'+kfhtml);  
  13. </script>  

 

效果

 

方式二.广告所有列表数据list.js,js处理,代替后端请求  http://hudeyong926.iteye.com/blog/1123139

Java代码   收藏代码
  1. var jsonarr=eval([{"pic":"/pop/images/1.gif","gname":"清纯美女气质大片","link":"http://www.xiumei.com/gallery/beauty/"},  
  2. {"pic":"/pop/images/2.gif","gname":"青春美少女诱人自拍","link":"http://www.xiumei.com/a/20110705/34392.shtml"},  
  3. {"pic":"/pop/images/3.gif","gname":"纯情MM魅惑美图","link":"http://www.xiumei.com/a/20110630/34096.shtml"},  
  4. {"pic":"/pop/images/4.gif","gname":"条纹控可爱制服写真","link":"http://www.xiumei.com/a/20110628/33836.shtml"},  
  5. {"pic":"/pop/images/5.gif","gname":"女神周韦彤湿身写真","link":"http://www.xiumei.com/a/20110429/30915.shtml"},  
  6. {"pic":"/pop/images/6.gif","gname":"夏季甜美感内衣外穿","link":"http://www.xiumei.com/a/20110715/35218.shtml"},  
  7. {"pic":"/pop/images/7.gif","gname":"清新日系少女发型","link":"http://www.xiumei.com/a/20110816/36976.shtml"},  
  8. {"pic":"/pop/images/8.gif","gname":"漂亮姐妹花服饰写真","link":"http://www.xiumei.com/a/20110817/37057.shtml"},  
  9. {"pic":"/pop/images/9.gif","gname":"矮个美女妙搭变高","link":"http://www.xiumei.com/a/20110818/37073.shtml"},  
  10. {"pic":"/pop/images/10.gif","gname":"90后MM可爱发型秀","link":"http://www.xiumei.com/a/20110817/36997.shtml"},  
  11. {"pic":"/pop/images/11.gif","gname":"周杰伦女友时尚搭配","link":"http://www.xiumei.com/a/20110815/36845.shtml"},  
  12. {"pic":"/pop/images/12.gif","gname":"姜妍魔鬼身材写真","link":"http://www.xiumei.com/a/20110727/35887.shtml"},  
  13. {"pic":"/pop/images/13.gif","gname":"性感内衣诱惑他心","link":"http://www.xiumei.com/a/20110816/36956.shtml"},  
  14. {"pic":"/pop/images/14.gif","gname":"最美温柔梨花头发型","link":"http://www.xiumei.com/a/20110815/36881.shtml"},  
  15. {"pic":"/pop/images/15.gif","gname":"自曝私密房事的女星","link":"http://www.xiumei.com/a/20110813/36798.shtml"},  
  16. {"pic":"/pop/images/16.gif","gname":"裸戏一夜成名的女星","link":"http://www.xiumei.com/a/20110805/36409.shtml"},  
  17. {"pic":"/pop/images/17.gif","gname":"色@情业觊觎的女星","link":"http://www.xiumei.com/a/20110813/36790.shtml"},  
  18. {"pic":"/pop/images/18.gif","gname":"Charming性感牛仔写真","link":"http://www.xiumei.com/a/20110715/35181.shtml"}]);  

 随机数组 html放在方法里面写html的 多行 效果比较自然

Java代码   收藏代码
  1. <script type="text/javascript">  
  2.     function setGameList(s, e, num) {  
  3.         var arr = new Array();  
  4.         for (i = s; i < e; i++) {  
  5.             arr[i] = i;  
  6.         }  
  7.         var temp_array = new Array();  
  8.         for (var index in arr) {  
  9.             temp_array.push(arr[index]);  
  10.         }  
  11.         var return_array = new Array();  
  12.         for (var i = 0; i < num; i++) {  
  13.             if (temp_array.length > 0) {  
  14.                 var arrIndex = Math.floor(Math.random() * temp_array.length);  
  15.                 return_array[i] = temp_array[arrIndex];  
  16.                 temp_array.splice(arrIndex, 1);  
  17.             } else {  
  18.                 break;  
  19.             }  
  20.         }  
  21.         var html = '';  
  22.         for (var i = 0; i < return_array.length; i++) {  
  23.             var j = return_array[i];  
  24.             html += '<li><a href="' + jsonarr[j].link + '" title="' + jsonarr[j].gname + '" target="_blank"><img src="' + jsonarr[j].pic + '"/><h3>' + jsonarr[j].gname + '</h3></a></li>';  
  25.         }  
  26.         $('#div_gamelist').append(html);  
  27.     }  
  28.   
  29.     setGameList(01815);  
  30. </script>  

 实例

方式3,随机改变图片的地址和链接的地址,页面的排版不改变

Java代码   收藏代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  2. "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf8">  
  6. <base href="http://hao.kkpk.com/"/>  
  7. <title>玩游戏送大奖!!</title>  
  8. <style>  
  9. .index_top:link {  
  10.     font-family: "宋体";  
  11.     color: #FFFFFF;  
  12.     font-size: 12px;  
  13.     font-weight: normal;  
  14.     line-height: 18px;  
  15.     text-decoration: none;  
  16. }  
  17. .index_top:visited {  
  18.     font-family: "宋体";  
  19.     color: #FFFFFF;  
  20.     font-size: 12px;  
  21.     font-weight: normal;  
  22.     line-height: 18px;  
  23.     text-decoration: none;  
  24. }  
  25. .index_top:hover {  
  26.     font-family: "宋体";  
  27.     color: #FFFFFF;  
  28.     text-decoration: underline;  
  29.     font-size: 12px;  
  30.     font-weight: normal;  
  31.     line-height: 18px;  
  32. }  
  33. .index_top:active {  
  34.     font-family: "宋体";  
  35.     color: #FFFFFF;  
  36.     text-decoration: underline;  
  37.     font-size: 12px;  
  38.     font-weight: normal;  
  39.     line-height: 18px;  
  40. }  
  41. .index_top1:link {  
  42.     font-family: "宋体";  
  43.     color: #FFFFFF;  
  44.     text-decoration: none;  
  45.     font-size: 12px;  
  46.     font-weight: normal;  
  47.     line-height: 18px;  
  48. }  
  49. .index_top1:visited {  
  50.     font-family: "宋体";  
  51.     color: #FFFFFF;  
  52.     text-decoration: none;  
  53.     font-size: 12px;  
  54.     font-weight: normal;  
  55.     line-height: 18px;  
  56. }  
  57. .index_top1:hover {  
  58.     font-family: "宋体";  
  59.     color: #FFFFFF;  
  60.     text-decoration: underline;  
  61.     font-size: 12px;  
  62.     font-weight: normal;  
  63.     line-height: 18px;  
  64. }  
  65. .index_top1:active {  
  66.     font-family: "宋体";  
  67.     color: #FFFFFF;  
  68.     text-decoration: underline;  
  69.     font-size: 12px;  
  70.     font-weight: normal;  
  71.     line-height: 18px;  
  72. }  
  73. .index_top2:link {  
  74.     font-family: "宋体";  
  75.     color: #FF5A00;  
  76.     text-decoration: none;  
  77.     font-size: 12px;  
  78.     font-weight: normal;  
  79.     line-height: 18px;  
  80. }  
  81. .index_top2:visited {  
  82.     font-family: "宋体";  
  83.     color: #FF5A00;  
  84.     text-decoration: none;  
  85.     font-size: 12px;  
  86.     font-weight: normal;  
  87.     line-height: 18px;  
  88. }  
  89. .index_top2:hover {  
  90.     font-family: "宋体";  
  91.     color: #FF5A00;  
  92.     text-decoration: underline;  
  93.     font-size: 12px;  
  94.     font-weight: normal;  
  95.     line-height: 18px;  
  96. }  
  97. .index_top2:active {  
  98.     font-family: "宋体";  
  99.     color: #FF5A00;  
  100.     text-decoration: underline;  
  101.     font-size: 12px;  
  102.     font-weight: normal;  
  103.     line-height: 18px;  
  104. }  
  105. .index_top3:link {  
  106.     font-family: "宋体";  
  107.     color: #666666;  
  108.     text-decoration: none;  
  109.     font-size: 12px;  
  110.     font-weight: bold;  
  111.     line-height: 18px;  
  112. }  
  113. .index_top3:visited {  
  114.     font-family: "宋体";  
  115.     color: #666666;  
  116.     text-decoration: none;  
  117.     font-size: 12px;  
  118.     font-weight: bold;  
  119.     line-height: 18px;  
  120. }  
  121. .index_top3:hover {  
  122.     font-family: "宋体";  
  123.     color: #666666;  
  124.     text-decoration: underline;  
  125.     font-size: 12px;  
  126.     font-weight: bold;  
  127.     line-height: 18px;  
  128. }  
  129. .index_top3:active {  
  130.     font-family: "宋体";  
  131.     color: #666666;  
  132.     text-decoration: underline;  
  133.     font-size: 12px;  
  134.     font-weight: bold;  
  135.     line-height: 18px;  
  136. }  
  137. .index_top4:link {  
  138.     font-family: "宋体";  
  139.     color: #666666;  
  140.     text-decoration: none;  
  141.     font-size: 12px;  
  142.     font-weight: normal;  
  143.     line-height: 18px;  
  144. }  
  145. .index_top4:visited {  
  146.     font-family: "宋体";  
  147.     color: #666666;  
  148.     text-decoration: none;  
  149.     font-size: 12px;  
  150.     font-weight: normal;  
  151.     line-height: 18px;  
  152. }  
  153. .index_top4:hover {  
  154.     font-family: "宋体";  
  155.     color: #666666;  
  156.     text-decoration: underline;  
  157.     font-size: 12px;  
  158.     font-weight: normal;  
  159.     line-height: 18px;  
  160. }  
  161. .index_top4:active {  
  162.     font-family: "宋体";  
  163.     color: #666666;  
  164.     text-decoration: underline;  
  165.     font-size: 12px;  
  166.     font-weight: normal;  
  167.     line-height: 18px;  
  168. }  
  169. body {  
  170.     margin-left: 0px;  
  171.     margin-top: 0px;  
  172.     margin-right: 0px;  
  173.     margin-bottom: 0px;  
  174. }  
  175. .style4 {font-size: 12px; color: #FF6600; }  
  176. .style5 {color: #666666; font-size: 14px;}  
  177. .style7 {color: #FFFFFF; font-size: 14px; }  
  178. </style>  
  179. <script src="jquery.js"></script>  
  180. <script src="list.js"></script>  
  181. <script>  
  182.     var adsize='270x200';  
  183.     location.href.replace(/\/(\d+)-(\d+)\./,function($1,$2,$3){adsize=$2+'x'+$3;});  
  184.       
  185.     jQuery(document).ready(function(){  
  186.         var pic = jQuery('a img');  
  187.         var link = jQuery('a');  
  188.         var num = jsonarr.length;  
  189.   
  190.         var index = Math.floor(Math.random()*num);  
  191.   
  192.         link.each(function(){  
  193.             if(jQuery('img',jQuery(this)).length == 0){  
  194.                 var obj = jsonarr[index%num];    
  195.                 jQuery(this).attr('href',obj.url+'&FromWebId=[wid]&AdSize='+adsize); //linkurl  
  196.                 index++;  
  197.             }  
  198.         })  
  199.   
  200.         pic.each(function(){  
  201.             var obj = jsonarr[index%num];  
  202.             jQuery(this).attr('src',obj.pic); //image url  
  203.             jQuery(this).parent().attr('href',obj.url+'&FromWebId=[wid]&AdSize='+adsize);//linkurl  
  204.             index++;  
  205.         })  
  206.     })  
  207. </script>  
  208. </head>  
  209. <body>  
  210. <table width="270" height="200"  border="0" cellpadding="0" cellspacing="0">  
  211.   <tr>  
  212.   
  213.     <td valign="top" bgcolor="#D7F1C2"><table width="100%"  border="0" cellspacing="1" cellpadding="1">  
  214.       <tr>  
  215.         <td><div align="center" class="index_top1"><a href="http://bbs.265g.com/tg/789/?FromWebId=%5Bwid%5D" target="_blank" class="index_top3">265游戏特辑 更多惊喜等着您</a></div></td>  
  216.       </tr>  
  217.       <tr>  
  218.         <td height="177" valign="top" bgcolor="#FFFFFF"><table width="100%"  border="0" cellspacing="0" cellpadding="0">  
  219.           <tr>  
  220.             <td height="3"></td>  
  221.   
  222.             </tr>  
  223.           <tr>  
  224.             <td><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank"><img src="images/4.gif" height="75" border="0"></a></div></td>  
  225.             <td><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank"><img src="images/8.gif" height="75" border="0"></a></div></td>  
  226.           </tr>  
  227.           <tr>  
  228.             <td height="20"><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank" class="index_top4"> 丝袜MM全程陪玩哟</a></div></td>  
  229.             <td><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank" class="index_top4">挑战你的视觉极限</a></div></td>  
  230.   
  231.           </tr>  
  232.           <tr>  
  233.             <td height="13"><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank"><img src="images/6.gif" height="75" border="0"></a></div></td>  
  234.             <td><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank"><img src="images/7.gif" height="75" border="0"></a></div></td>  
  235.           </tr>  
  236.           <tr>  
  237.             <td height="2"></td>  
  238.             </tr>  
  239.         </table></td>  
  240.   
  241.       </tr>  
  242.     </table></td>  
  243.   </tr>  
  244. </table>  
  245. </body>  
  246. </html>  

 效果如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值