一、Window
1、所有浏览器都支持window对象,它表示浏览器窗口,所有JavaScript全局对象、函数以及变量均自动成为window对象的成员,全局变量是window对象的属性,全局函数是window对象的方法,甚至HTML DOM的document也是window对象的属性之一
window.document.getElementById("header");
2、window尺寸:有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)
①对于Internet Explorer、Chrome、Firefox、Opera以及Safari:window.innerHeight和window.innerWidth分别获取浏览器窗口的内部高度和宽度
②对于Internet Explorer 8、7、6、5:document.documentElement.clientHeight和document.documentElement.clientWidth
③document.body.clientHeight和document.body.clientWidth
<p id="demo"></p>
<script>
var w=window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h=window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
x=document.getElementById("demo");
x.innerHTML="浏览器的内部窗口宽度:" + w + ",高度:" + h + "。"
</script>
二、Window.Screen
window.screen对象包含有关用户屏幕的信息,和document一样,在编写时可以不使用window这个前缀,比如screen.availHeight和screen.availWidth分别是获取访问者屏幕的可用高度和宽度
三、Window.Location
1、window.location对象用于获得当前页面的url,并把浏览器重定向到新的界面,同document,在编写时可以不使用window这个前缀
2、属性:①location.href:返回当前页面的url ②location.pathname:返回url的路径名 ③location.assign(url):加载新的文档 ④location.hostname:返回web主机的域名 ⑤location.port:返回web主机的端口 ⑥location.protocol:返回所使用的web协议
四、Window.History
1、window.history包含浏览器的历史,同上在编写时也可不使用window这个前缀,为了保护用户隐私,对JavaScript访问对象的方法做出了限制
2、方法:①history.back():与在浏览器点击后退按钮相同,加载历史列表中的前一个url ②history.forward():与在浏览器点击向前按钮相同,加载历史列表中的下一个url
五、Window.Navigator
1、window.navigator对象包含有关访问者浏览器的信息
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>";
来在navigator对象的信息具有误导性,不应该用于检测浏览器版本,因为navigator数据可被浏览器使用者更改,浏览器无法报告晚于浏览器发布的新操作系统
六、PopupAlert消息框
1、警告框:用于确保用户可以得到某些信息,当警告框出现后,用户需要点击确认按钮才能继续进行操作
语法:
alert("再次向您问好!在这里,我们向您演示" + '\n' + "如何向警告框添加折行。")
2、确认框:用于使用户可以验证或者接受某些信息,当确认框出现后,用户需要点击确认或者取消按钮才能继续进行操作,如果用户点击确认,则返回值为true,点击取消则返回false
语法:
<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button!");
if (r==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
}
</script>
</head>
<body>
<input type="button" οnclick="show_confirm()" value="Show a confirm box" />
</body>
</html>
3、提示框:用于提示用户在进入页面前输入某个值,当提示框出现后,用户需要输入某个值,然后点击确认或者取消按钮才能继续操作,如果用户点击确认,则返回值为输入的值,如果点击取消,则返回值为null
语法:
<html>
<head>
<script type="text/javascript">
function disp_prompt()
{
var name=prompt("请输入您的名字","Bill Gates")
if (name!=null && name!="")
{
document.write("你好!" + name + " 今天过得怎么样?")
}
}
</script>
</head>
<body>
<input type="button" οnclick="disp_prompt()" value="显示提示框" />
</body>
</html>
七、JavaScript计时
通过使用JavaScript,我么有能力做到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行,我们称之为计时事件,在JavaScript中使用计时事件是很容易的,两个关键方法是:
①setTimeout():未来的某时执行代码,这个方法会返回某个值,如果你要取消这个setTimeout(),可以使用这个变量名来指定它
语法:
var t=setTimeout("javascript语句",毫秒)
②clearTimeout():取消setTimeout(),
实例:
<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}
function stopCount()
{
c=0;
setTimeout("document.getElementById('txt').value=0",0);
clearTimeout(t);
}
</script>
</head>
<body>
<form>
<input type="button" value="开始计时!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="停止计时!" onClick="stopCount()">
</form>
<p>请点击上面的“开始计时”按钮来启动计时器。输入框会一直进行计时,从 0 开始。点击“停止计时”按钮可以终止计时,并将计数重置为 0。</p>
</body>
</html>
八、JavaScript Cookies
cookies是存储于访问者的计算机中的变量,每当同一台计算机通过浏览器请求某个页面时,就会发送这个cookie,你可以使用JavaScript来创建和取回cookie的值
<html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
function checkCookie()
{
username=getCookie('username')
if (username!=null && username!="")
{alert('Welcome again '+username+'!')}
else
{
username=prompt('Please enter your name:',"")
if (username!=null && username!="")
{
setCookie('username',username,365)
}
}
}
</script>
</head>
<body onLoad="checkCookie()">
</body>
</html>