javascript在主页中的小应用
时间:2007-02-17 来源:PHP爱好者
1.状态栏显示时间
将下代码copy入你网页的<head>和</head>中的合适地方即可。
<SCRIPT LANGUAGE=JAVASCRIPT>
<!--
var timerID = null;
var timerRunning = false;
function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}
function showtime () {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "" + ((hours >12) ? hours -12 :hours)
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
timeValue += (hours >= 12) ? " P.M." : " A.M."
window.status = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}
function startclock () {
stopclock();
showtime();
}
-->
</SCRIPT>
将你Html文件的<body>改为如下:
<BODY onLoad="startclock()">
2.辨别浏览器
实现这个功能的原代码如下:
<script language="JavaScript"><!--
document.write("<CENTER>您的浏览器是: " +navigator.appName + " " + navigator.appVersion +"<CENTER>")
// --></script>
将其放入html里就可以了。(此程序对netscape3.x无效)
3。弹出窗口的制作
有时候你需要在进入网站的同时,弹出一个小窗口,告诉来访者最新的消息或则调查,广告之类的东西,实现的方法很简单,将下面的代码插入<head></head>之间就可以了:
<script language="JavaScript">
<!--
var gt = unescape('%3e');
var popup = null;
var over = "Launch Pop-up Navigator";
popup = window.open('', 'popupnav', 'width=200,height=150,resizable=0,scrollbars=auto');
if (popup != null) {
if (popup.opener == null) {
popup.opener = self;
}
popup.location.href = 'pop.htm';
}
// -->
</script>
4。禁止查看源代码
可能你看到某些好的网页效果。却发现不允许你查看原代码,是如何实现的呢?
只要在<head>和</head>之间加入下面这段代码就可以了:(可以用鼠标右键试试)
<SCRIPT language=javascript>
function click() {
if (event.button==2) {y
alert('对不起,禁止使用此功能^_^.')
}
}
document.onmousedown=click
</SCRIPT>
此方法只对IE有效。但它并不能真正阻止访问者用其他方法看到页面源代码,所以你只能把它做为对想看源码的来访者一个简单的警告。
5。最后更新日期
<script>
document.write("最后更新日期:"+document.lastModified+"<p>")
</script>
6.访问次数显示
下代码加入<HEAD>区:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var expDays = 30; //设置COOKIES时间
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function amt(){
var count = GetCookie('count')
if(count == null) {
SetCookie('count','1')
return 1
}
else {
var newcount = parseInt(count) + 1;
DeleteCookie('count')
SetCookie('count',newcount,exp)
return count
}
}
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
// End -->
</SCRIPT>
</head>
如下代码加入<BODY>区:
<body>
<script language="JavaScript">
<!-- Begin
document.write("您已经来此" + amt() + "次了!")
// End -->
</script>
php爱好者站 http://www.phpfans.net c/vc/c++/java.
将下代码copy入你网页的<head>和</head>中的合适地方即可。
<SCRIPT LANGUAGE=JAVASCRIPT>
<!--
var timerID = null;
var timerRunning = false;
function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}
function showtime () {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "" + ((hours >12) ? hours -12 :hours)
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
timeValue += (hours >= 12) ? " P.M." : " A.M."
window.status = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}
function startclock () {
stopclock();
showtime();
}
-->
</SCRIPT>
将你Html文件的<body>改为如下:
<BODY onLoad="startclock()">
2.辨别浏览器
实现这个功能的原代码如下:
<script language="JavaScript"><!--
document.write("<CENTER>您的浏览器是: " +navigator.appName + " " + navigator.appVersion +"<CENTER>")
// --></script>
将其放入html里就可以了。(此程序对netscape3.x无效)
3。弹出窗口的制作
有时候你需要在进入网站的同时,弹出一个小窗口,告诉来访者最新的消息或则调查,广告之类的东西,实现的方法很简单,将下面的代码插入<head></head>之间就可以了:
<script language="JavaScript">
<!--
var gt = unescape('%3e');
var popup = null;
var over = "Launch Pop-up Navigator";
popup = window.open('', 'popupnav', 'width=200,height=150,resizable=0,scrollbars=auto');
if (popup != null) {
if (popup.opener == null) {
popup.opener = self;
}
popup.location.href = 'pop.htm';
}
// -->
</script>
4。禁止查看源代码
可能你看到某些好的网页效果。却发现不允许你查看原代码,是如何实现的呢?
只要在<head>和</head>之间加入下面这段代码就可以了:(可以用鼠标右键试试)
<SCRIPT language=javascript>
function click() {
if (event.button==2) {y
alert('对不起,禁止使用此功能^_^.')
}
}
document.onmousedown=click
</SCRIPT>
此方法只对IE有效。但它并不能真正阻止访问者用其他方法看到页面源代码,所以你只能把它做为对想看源码的来访者一个简单的警告。
5。最后更新日期
<script>
document.write("最后更新日期:"+document.lastModified+"<p>")
</script>
6.访问次数显示
下代码加入<HEAD>区:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var expDays = 30; //设置COOKIES时间
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function amt(){
var count = GetCookie('count')
if(count == null) {
SetCookie('count','1')
return 1
}
else {
var newcount = parseInt(count) + 1;
DeleteCookie('count')
SetCookie('count',newcount,exp)
return count
}
}
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
// End -->
</SCRIPT>
</head>
如下代码加入<BODY>区:
<body>
<script language="JavaScript">
<!-- Begin
document.write("您已经来此" + amt() + "次了!")
// End -->
</script>
php爱好者站 http://www.phpfans.net c/vc/c++/java.
相关阅读 更多 +