`
hudeyong926
  • 浏览: 2028232 次
  • 来自: 武汉
社区版块
存档分类
最新评论

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,每当文档被载入时,我们希望能随机地显示这些图片。

<script language="JavaScript"><!--  
document.write('<img src="../images/banner' + rand(10) + '.gif" width=400 height=40>');  
//--></script>  

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

<script type="text/javascript">
    function getkfArrayItems(s, e, num) {
        var arr = new Array();
        for (i = s; i < e; i++) {
            arr[i] = i;
        }
        var temp_array = new Array();
        for (var index in arr) {
            temp_array.push(arr[index]);
        }
        var return_array = new Array();
        for (var i = 0; i < num; i++) {
            if (temp_array.length > 0) {
                var arrIndex = Math.floor(Math.random() * temp_array.length);
                return_array[i] = temp_array[arrIndex];
                temp_array.splice(arrIndex, 1);//入栈后删除 即去重
            } else {
                break;
            }
        }
		return return_array;
    }
	//得到数组的随机元素12个
    kf_array = getkfArrayItems(0, 20, 12);
</script>

广告所有列表ads.js

<!--
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>");
-->

随机广告列表

<script src="jquery.js" type="text/javascript"></script>
<div class="top_kaifu"></div>
<div id="div_zxkf_h" style="display: none">
	<script src="ads.js" type="text/javascript"></script>
</div>
<script type="text/javascript">
	var kfhtml='';
	for (i = 0; i < kf_array.length; i++)
	{
		kfhtml+='<div class="gimg_li">'+$('#div_zxkf_h>div').eq(kf_array[i]).html()+'</div>';
	}
	$('.top_kaifu').append('<i>最新开服</i>'+kfhtml);
</script>

 

效果

 

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

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

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

<script type="text/javascript">
    function setGameList(s, e, num) {
        var arr = new Array();
        for (i = s; i < e; i++) {
            arr[i] = i;
        }
        var temp_array = new Array();
        for (var index in arr) {
            temp_array.push(arr[index]);
        }
        var return_array = new Array();
        for (var i = 0; i < num; i++) {
            if (temp_array.length > 0) {
                var arrIndex = Math.floor(Math.random() * temp_array.length);
                return_array[i] = temp_array[arrIndex];
                temp_array.splice(arrIndex, 1);
            } else {
                break;
            }
        }
        var html = '';
        for (var i = 0; i < return_array.length; i++) {
            var j = return_array[i];
            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>';
        }
        $('#div_gamelist').append(html);
    }

    setGameList(0, 18, 15);
</script>

 实例

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<base href="http://hao.kkpk.com/"/>
<title>玩游戏送大奖!!</title>
<style>
.index_top:link {
	font-family: "宋体";
	color: #FFFFFF;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
	text-decoration: none;
}
.index_top:visited {
	font-family: "宋体";
	color: #FFFFFF;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
	text-decoration: none;
}
.index_top:hover {
	font-family: "宋体";
	color: #FFFFFF;
	text-decoration: underline;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top:active {
	font-family: "宋体";
	color: #FFFFFF;
	text-decoration: underline;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top1:link {
	font-family: "宋体";
	color: #FFFFFF;
	text-decoration: none;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top1:visited {
	font-family: "宋体";
	color: #FFFFFF;
	text-decoration: none;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top1:hover {
	font-family: "宋体";
	color: #FFFFFF;
	text-decoration: underline;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top1:active {
	font-family: "宋体";
	color: #FFFFFF;
	text-decoration: underline;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top2:link {
	font-family: "宋体";
	color: #FF5A00;
	text-decoration: none;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top2:visited {
	font-family: "宋体";
	color: #FF5A00;
	text-decoration: none;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top2:hover {
	font-family: "宋体";
	color: #FF5A00;
	text-decoration: underline;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top2:active {
	font-family: "宋体";
	color: #FF5A00;
	text-decoration: underline;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top3:link {
	font-family: "宋体";
	color: #666666;
	text-decoration: none;
	font-size: 12px;
	font-weight: bold;
	line-height: 18px;
}
.index_top3:visited {
	font-family: "宋体";
	color: #666666;
	text-decoration: none;
	font-size: 12px;
	font-weight: bold;
	line-height: 18px;
}
.index_top3:hover {
	font-family: "宋体";
	color: #666666;
	text-decoration: underline;
	font-size: 12px;
	font-weight: bold;
	line-height: 18px;
}
.index_top3:active {
	font-family: "宋体";
	color: #666666;
	text-decoration: underline;
	font-size: 12px;
	font-weight: bold;
	line-height: 18px;
}
.index_top4:link {
	font-family: "宋体";
	color: #666666;
	text-decoration: none;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top4:visited {
	font-family: "宋体";
	color: #666666;
	text-decoration: none;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top4:hover {
	font-family: "宋体";
	color: #666666;
	text-decoration: underline;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
.index_top4:active {
	font-family: "宋体";
	color: #666666;
	text-decoration: underline;
	font-size: 12px;
	font-weight: normal;
	line-height: 18px;
}
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
.style4 {font-size: 12px; color: #FF6600; }
.style5 {color: #666666; font-size: 14px;}
.style7 {color: #FFFFFF; font-size: 14px; }
</style>
<script src="jquery.js"></script>
<script src="list.js"></script>
<script>
	var adsize='270x200';
	location.href.replace(/\/(\d+)-(\d+)\./,function($1,$2,$3){adsize=$2+'x'+$3;});
	
	jQuery(document).ready(function(){
		var pic = jQuery('a img');
		var link = jQuery('a');
		var num = jsonarr.length;

		var index = Math.floor(Math.random()*num);

		link.each(function(){
			if(jQuery('img',jQuery(this)).length == 0){
				var obj = jsonarr[index%num];  
				jQuery(this).attr('href',obj.url+'&FromWebId=[wid]&AdSize='+adsize); //linkurl
				index++;
			}
		})

		pic.each(function(){
			var obj = jsonarr[index%num];
			jQuery(this).attr('src',obj.pic); //image url
			jQuery(this).parent().attr('href',obj.url+'&FromWebId=[wid]&AdSize='+adsize);//linkurl
			index++;
		})
	})
</script>
</head>
<body>
<table width="270" height="200"  border="0" cellpadding="0" cellspacing="0">
  <tr>

    <td valign="top" bgcolor="#D7F1C2"><table width="100%"  border="0" cellspacing="1" cellpadding="1">
      <tr>
        <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>
      </tr>
      <tr>
        <td height="177" valign="top" bgcolor="#FFFFFF"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="3"></td>

            </tr>
          <tr>
            <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>
            <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>
          </tr>
          <tr>
            <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>
            <td><div align="center"><a href="http://bbs.265g.com/tg/789/?FromWebId=[wid]" target="_blank" class="index_top4">挑战你的视觉极限</a></div></td>

          </tr>
          <tr>
            <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>
            <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>
          </tr>
          <tr>
            <td height="2"></td>
            </tr>
        </table></td>

      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>

 效果如下

 
 
  • 大小: 34.5 KB
  • 大小: 181.1 KB
  • 大小: 471.5 KB
  • 大小: 165.5 KB
  • 大小: 19 KB
分享到:
评论

相关推荐

    js json 重新随机重组排序方法demo

    在JavaScript中,处理JSON数据是一项常见的任务,尤其是在前端开发中。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它易于人阅读和编写,同时也易于机器解析和生成。当我们需要对JSON对象中的...

    js 问题随机选项实例,mock接口数据,json格式,ajax

    在JavaScript(JS)开发中,经常遇到需要模拟(mock)数据的情况,特别是在前端与后端接口还未完全对接时。这个实例主要关注如何使用JS来创建一个具有随机选项的问题,并通过JSON格式来存储这些数据,最后利用AJAX...

    js随机红包的生成页面

    综上所述,"js随机红包的生成页面"涉及到的主要知识点有:JavaScript随机数生成、红包算法、AJAX通信、JSON数据交换、前端交互设计以及基础的HTTP请求和响应流程。通过这个项目,开发者可以提升JavaScript编程能力,...

    echarts地图json数据

    总的来说,理解和使用ECharts地图JSON数据是前端开发中进行数据可视化的关键技能之一。它涉及到JavaScript基础、JSON数据格式、ECharts API等多个知识点。通过结合JSON地图数据,开发者可以创建出互动性强、视觉效果...

    echarts地图js及json数据(全国及省)

    在"echarts地图js及json数据(全国及省)"这个主题中,我们将深入探讨如何利用ECharts与JSON数据结合,来实现全国及各省的地图渲染。 首先,ECharts中的地图依赖于特定的地理JSON数据。JSON(JavaScript Object ...

    基于node.js的随机点名器

    在实际使用中,用户可以通过浏览器访问点名器的URL,前端页面会向Node.js服务器发送请求,服务器则处理请求并随机选择一个名字,最后将结果返回给前端展示。整个过程充分利用了Node.js的非阻塞I/O特性和JavaScript的...

    Python+HTML随机点名抽奖源码模板抽奖系统

    总结,这个“Python+HTML随机点名抽奖源码模板”项目涉及到的技术知识点包括Python的随机数生成、HTML和Bootstrap构建的响应式界面、JavaScript实现的前端交互逻辑以及服务器与客户端的通信。这个项目提供了一个实际...

    js实现随机点名程序.rar

    在本文中,我们将深入探讨如何使用JavaScript实现一个随机点名程序。这个程序主要涉及HTML、CSS和JavaScript这三种前端技术,它们共同构建了一个用户友好的交互界面和动态功能。首先,我们来了解一下JavaScript的...

    全国省份、城市JSON

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它采用完全独立于语言的文本格式,被广泛用于网络服务间的数据交互。全国省份、城市JSON数据是指将中国所有省份和城市的信息以JSON格式编码存储的...

    jQuery查看json格式数据插件viewer.js.zip

    标题中的“jQuery查看json格式数据插件viewer.js.zip”指的是一个使用jQuery库开发的插件,主要用于在网页中查看和解析JSON(JavaScript Object Notation)格式的数据。JSON是一种轻量级的数据交换格式,广泛用于...

    前端项目-Mock.js.zip

    Mock.js 的核心功能是生成模拟数据,它可以生成各种类型的随机数据,如字符串、数字、日期、布尔值等,甚至可以模拟复杂的JSON结构,这对于快速搭建前端界面、编写单元测试或者进行功能验证非常有帮助。它的工作原理...

    Banner随机显示

    这可以通过JSON格式传输数据,前端使用JavaScript库如jQuery或者现代的React、Vue.js等进行解析和展示。 4. 点击事件处理:为Banner添加点击事件监听器,当用户点击Banner时,触发预设的链接跳转。这在前端...

    Json数据的HTML5音乐播放器

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,常用于前端与后端的数据交互。HTML5音乐播放器则是利用HTML5的新特性,如`&lt;audio&gt;`标签,实现网页内的音频播放功能。本项目结合两者,通过JSON数据...

    前端基于js的背单词小程序

    在前端开发领域,JavaScript(简称js)是一种不可或缺的编程语言,尤其在构建交互性强、用户体验良好的Web应用时。本项目以js为技术核心,创建了一个用于帮助用户背诵单词的小程序。这个小程序能够提供一个便捷的...

    PHP随机播放抖音快手小姐姐源码

    【PHP随机播放抖音快手小姐姐源码】是一款基于PHP开发的软件,主要用于实现对抖音和快手平台上小姐姐视频的随机播放功能。这款源码充分利用了抖音和快手的公开接口,虽然当前提供的接口可能已失效,但用户可以自行...

    前端开发小游戏

    在这个“点水果”游戏中,开发者需要利用js和jQuery来创建动态的水果元素,并在屏幕上随机显示。当用户点击水果时,游戏会判断点击是否正确,然后更新分数或显示游戏结束的状态。这涉及到JavaScript的事件监听和处理...

    Flex与JSON及XML的互操作

    2. **JSON(JavaScript Object Notation)与XML(Extensible Markup Language)**:两种常用的数据交换格式,在Flex应用中用于客户端与服务器间的数据传输。 3. **互操作性**:Flex与JSON、XML之间的数据交换和处理...

    使用mock.js随机数据和使用express输出json接口的实现方法

    Mock.js是一个非常实用的库,它允许我们创建随机数据用于前端调试,而Express框架则可以让开发者在Node.js环境下快速搭建Web服务,以输出JSON格式的数据接口。本文将详细介绍如何使用Mock.js生成随机数据,并使用...

Global site tag (gtag.js) - Google Analytics