javascript标题栏效果:如何显示当前日期及当前时间

发布时间:2021-01-15编辑:脚本学堂
有关javascript操作标题栏的几个例子,javascript在标题栏上显示当前日期、当前时间的方法,javascript操作时间及dom节点,以及javascript获取网页标题的例子。

一、javascript实现在标题栏上显示当前日期

例子:
 

复制代码 代码示例:
<script language="JavaScript">
<!--
function writeIt() {
// getDate
var now = new Date();
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
// quadYear
function y2k(number){return (number < 1000) ? number + 1900 : number;}
// glueIt
today =  days[now.getDay()] + " " +
months[now.getMonth()] + ", " +
 date + " " +
 (y2k(now.getYear())) ;
   if(document.all || document.getElementById){ // Browser Check
      document.title = today.toString();
   }else{
      self.status = today.toString(); // Default to status.
   }
}
writeIt();
//-->
</script>

二、javascript在浏览器标题栏上显示当前日期和时间的方法

javascript在浏览器标题栏上显示当前日期和时间的方法

将这段脚本放到head区即可:
 

复制代码 代码示例:
<script language="javascript">
<!--
function resetIt() {
// Calculate Time
var timerID = null;
var timerRunning = false;
if(timerRunning)
  clearTimeout(timerID);
  timerRunning = false;
// getTime
var timeNow = new Date();
var hours = timeNow.getHours();
var minutes = timeNow.getMinutes();
var seconds = timeNow.getSeconds();
var timeValue = "" + ((hours >12) ? hours -12 :hours)
timeValue = ((timeValue <10)? "0":"") + timeValue
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
timeValue += (hours >= 12) ? " PM" : " AM"
timerID = setTimeout("resetIt()",100);
timerRunning = true;
// getDate
var dateNow = new Date();
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((dateNow.getDate()<10) ? "0" : "")+ dateNow.getDate();
function y2k(number){return (number < 1000) ? number + 1900 : number;}
// compileIt
today = timeValue + " " + days[dateNow.getDay()] + " " +
months[dateNow.getMonth()] + ", " +
date + " " +
(y2k(dateNow.getYear()));
  if(document.all || document.getElementById){ // Browser Check
   document.title = today.toString();
  }else{
   self.status = today.toString(); // Default to status.
  }
}
resetIt();
//-->
</script>

三、javascript 获取网页标题

例子:
 

复制代码 代码示例:
<!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>javascript_获取网页标题_www.jb200.com</title>
<script language="javascript">
function get_pagetitle(split_chr,new_pagetit)
{
 var titleitems,return_title;
 var the_pagetitle=document.title;
 titleitems=the_pagetitle.split(split_chr);
 return_title=the_pagetitle;
 if(titleitems[1])
 {
  return_title=the_pagetitle.replace(titleitems[1],new_pagetit)
 }
 else
 {
  if(new_pagetit)
  {
   return_title=the_pagetitle & split_chr & new_pagetit;
  }
 }
 alert(return_title);
}
</script>
</head>
<body>
<input value="测试"  type="button" onclick="javascript:get_pagetitle('_','=');" />
</body>
</html>