最近用到的jscript代码整理

jscript代码功能十分强大,不仅实现网页一些特殊效果,同时能实现 动态网页中的部分功能。以下是我最近写网页用到的代码:

实现框架随着嵌套网页的高度自动变换高度和宽度。

<script language="javascript">
function mm(str)
{
var e = eval("document.all."+ str);
e.style.height = document.body.scrollHeight;
e.style.width = document.body.scrollWidth;
}
</script>
function TuneHeight(fm_name,fm_id){
var par=document.
var frm=document.getElementById(fm_id);

var subWeb=document.frames?document.frames[fm_name].document:frm.contentDocument;

if(frm != null && subWeb != null){
frm.style.height = subWeb.documentElement.scrollHeight+"px";
frm.style.width = subWeb.documentElement.scrollWidth+"px";
}
}

在表格里设置代码
<tr style="height:expression(js.document.body.scrollHeight)" >
<td >
<iframe name="js" width="100%" height="100%" id="js" marginwidth="0" marginheight="0" frameborder="0" allowTransparency="true"></iframe>
</td>
</tr>

实现点击链接在框架中加载网页内容file1表示要加载的网页名字,js表示框架的id就是上面代码的格式。
调用方式是<a href="javascript:lloader('waitforingload.html')">
function lloader(file1)
{
document.frames['js'].location=file1
}

显示日期和时间,
<script language="javascript">
<!--
calendar = new Date();
day = calendar.getDay();
month = calendar.getMonth();
date = calendar.getDate();
year = calendar.getYear();
if (year< 100) year = 1900 + year;
cent = parseInt(year/100);
g = year % 19;
k = parseInt((cent - 17)/25);
i = (cent - parseInt(cent/4) - parseInt((cent - k)/3) + 19*g + 15) % 30;
i = i - parseInt(i/28)*(1 -
parseInt(i/28)*parseInt(29/(i+1))*parseInt((21-g)/11));
j = (year + parseInt(year/4) + i + 2 - cent + parseInt(cent/4)) % 7;
l = i - j;
emonth = 3 + parseInt((l + 40)/44);
edate = l + 28 - 31*parseInt((emonth/4));
emonth--;
var dayname = new Array ("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
var monthname =
new Array ("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"
);
document.write("<font color=red> "+year +"年");
document.write(monthname[month]);
document.write(date + "日"+" <br>&nbsp;&nbsp;&nbsp;&nbsp;");
document.write(dayname[day]+" "+"<br>");
//-->
</script>
<script language="javascript">
<!--
document.write("<span id='clock'></span>");
var now,hours,minutes,seconds,timeValue;
function showtime(){
now = new Date();
hours = now.getHours();
minutes = now.getMinutes();
seconds = now.getSeconds();
timeValue = (hours >= 12) ? "下午 " : "上午 ";
timeValue += ((hours > 12) ? hours - 12 : hours) + "点";
timeValue += ((minutes <10)?"0":"") + minutes+"分";
timeValue += ((seconds <10)?"0":"") + seconds+"秒</font>";
clock.innerHTML = timeValue;
setTimeout("showtime()",100);
}
showtime();
//-->
</script>

天气预报
显示天气预报的代码
下面的所用说明,在你要显示天气的网页中加入下面的调用代码,注意修改参数,因为我是修改过代码,自己建立了新模板,所用调用的模板是hhht,就是skin=hhht,默认是default模板。模板的作用是调整显示方式和要显示内容,提供iframe调用frame.htm文件,访问weather/skin/hhht/template.htm,这里调用了do.js文件来访问天气服务器,返回的参数在weather/skin/hhht/template.htm中以你要求的格式显示,我改过的模板只显示今天的天气情况,默认的显示今天和明天天气。


调用代码:
<iframe src="weather/frame.htm?skin=hhht&cityId=53463" width="145" height="75" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" name="265"></iframe>

在weather/frame.htm中是调用网页,这段代码的价值不仅仅实现显示天气,关键是在动态网页中实现参数的传递,同时根据服务器返回的参数实现动态显示天气,这仅仅是用静态网页来实现。
?后的skin表示用那个模板,我用的是hhht呼和浩特的模板,cityid表示城市代码,这里是呼和浩特的代码。

weather/frame.htm的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>天气预报</title>
<style>* {margin:0px; padding:0px;}</style>
</head>
<body>
<iframe allowtransparency="true" id="frame" scrolling="no" frameborder="0" marginwidth="1" marginheight="1" height="70" width="298"></iframe>
<script type="text/javascript" src="do/weather.js"></script>
</body>
</html>

weather/do/weather.js代码
var server = 'http://weather.mzoe.com';
var post = getPostInfo();
if(location.href.indexOf('frame.htm') > 0)
{
var skin = post['skin'] ? post['skin'] : 'default';
var cityId = post['cityId'] ? post['cityId'] : getCookie('wc');
var site = getDomain().replace(/^/./, '');$('frame').src = cityId ? 'skin/'+skin+'/template.htm?cityId='+cityId+'&site='+site : server + '/do?skin='+skin+'&site='+site;
}
else
{
var label = getCookie('label');
if(label)
{showWeather(label);}
else{
var post = getPostInfo();loadJs(server + '/simple/'+post['cityId']+'.js');
if(getCookie('wc') == null)setCookie('wc', post['cityId'], 5184000, '/', getDomain());
}
}
function getCookie(name)
{
var cookie_start = document.cookie.indexOf(name);
var cookie_end = document.cookie.indexOf(";", cookie_start);
return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}
function setCookie(cookieName, cookieValue, seconds, path, domain, secure)
{
var expires = new Date();expires.setTime(expires.getTime() + seconds*1000);document.cookie = escape(cookieName) + '=' + escape(cookieValue)+ (expires ? '; expires=' + expires.toGMTString() : '')+ (path ? '; path=' + path : '/')+ (domain ? '; domain=' + domain : '')+ (secure ? '; secure' : '');
}
function getDomain()
{
var domain;var tmp = top.location.host.split('.');
var len = tmp.length;
if(len == 3)
{domain = '.'+tmp[len-2]+'.'+tmp[len-1];}
else if(len == 4)
{domain = '.'+tmp[len-3]+'.'+tmp[len-2]+'.'+tmp[len-1];}
else{domain = top.location.host;}
return domain;
}
function $(id)
{
return document.getElementById(id);
}
function getPostInfo()
{
var re = /([^/?&]+)=([^&]*)/g;
var post = [];
while(arr = re.exec(location.href))
{
post[arr[1]] = arr[2];}return post;
}
function stitution(templet, label)
{
var outPut = templet;label = label.split('|');
label = {url:server + label[0],city:label[1],weather:label[2],description:label[3],temperature:label[4],wind:label[5],weather2:label[6],description2:label[7],temperature2:label[8],wind2:label[9]};
for(var key in label)
{
outPut = outPut.replace(new RegExp('///{'+key+'///}', 'g'), label[key]);
}
return outPut;
}
function loadJs(url)
{
var oHead = document.getElementsByTagName('HEAD').item(0);
var oScript = document.createElement("script");oScript.type = "text/javascript";oScript.src = url;oHead.appendChild(oScript);
}
function showWeather(label)
{
var outPut = $('weather').innerHTML;$('weather').innerHTML = stitution(outPut, label);$('weather').style.display = '';
if(!getCookie('label'))setCookie('label', label, 2000, '/', getDomain());
}

weather/skin/hhht/template.htm代码
<html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>天气预报</title>
<link type="text/css" rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="weather" style="display:none;">
<!-- 模板开始(注:模板以外代码请不要做任何修改) -->
<table height="68" width="140" border="0" cellpadding="0" cellspacing="0" align="left" class="ba9">
<tr>
<td height="30" colspan="2" align="center" class="b19"><a href="{url}" target="_blank">{city} </a><font color="#FF0000">今天</font></td>
<tr>
<td width="50%">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td><img src="images/{weather}" /></td>
<td>{temperature}</td>
</tr>
</table></td>
</tr>
<tr>
<td>{wind}</td>
</tr>
</td>
</tr>
</table>
<!-- 模板结束 -->
</div>
</body>
<script type="text/javascript" src="../../do/weather.js"></script>
</html>

默认的模板

<html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>天气预报</title>
<link type="text/css" rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="weather" style="display:none;">
<!-- 模板开始(注:模板以外代码请不要做任何修改) -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="right"><a title="点击查看更多信息" href="{url}" target="_blank" id="link">{city}<img src="images/{weather}" />&nbsp;{temperature}</a></td>
</tr>
</table>
<!-- 模板结束 -->
</div>
</body>
<script type="text/javascript" src="../../do/weather.js"></script>
</html>

模板下载和说明:
http://weather.mzoe.com

根据上面的代码编写对视频文件的动态调用。
说明,在调用代码总用urlget中传递要播放的文件名,把参数用play.html?rm=1.rm格式送到play。html中,弹出新网页,在play函数总通过正则表达式得到文件名的参数rm后的值,在play.html中调用播放代码play()实现播放。

调用代码,脚本函数在do.js里面:
<a href="javascript:urlgetput('1.rm')">在线观看 </a>

play.html代码:

<!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=gb2312">
<title>录像</title>
</head>
<script type="text/javascript" src="do.js"></script>
<body>
<table width="100%">
<tr>
<td>
<table width="98%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="176" align=center>
<script type="text/javascript">
play();
</script>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

do.js文件代码
function getQueryStringRegExp(name)
{
var reg = new RegExp("(^|//?|&)"+ name +"=([^&]*)(//s|&|$)", "i");
if (reg.test(location.href))
return unescape(RegExp.$2.replace(//+/g, " "));
return "";
};

function urlgetput(s)
{
if(s!=null)
{
str="play.html?rm="+s;
//parent.location.href=str; _fcksavedurl="str;"
window.open(str);
}
}

function play()
{
var str="<EMBED src="+getQueryStringRegExp('rm')+' width=350 height=350 type=audio/x-pn-realaudio-plugin console="Clip1" controls="IMAGEWINDOW,ControlPanel,StatusBar" autostart="true"></EMBED>'
document.write(str);
}

这样我们就可以在静态网页中传递参数同时实现动态网页的功能,如果服务器有对mdb文件写的权限,甚至可以用静态网页直接操作数据库,实现对访问人数的统计,和对动态新闻的调用。

版权声明:本文为博主原创文章,未经博主允许不得转载。

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页