JS Window
window对象
所有浏览器都支持window对象。它表示浏览器窗口。
所有JavaScript全局对象、函数以及变量均自动成为window对象的成员。
全局变量是window对象的属性。
全局函数是window对象的方法。
甚至HTML DOM的document也是window对象的属性之一:
window.document.getElementById("header");
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
实用的 JavaScript 方案(涵盖所有浏览器):
var w = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
window方法
- window.open() - 打开新窗口
- window.close() - 关闭当前窗口
- window.moveTo() - 移动当前窗口
- window.resizeTo() - 调整当前窗口的尺寸
window.screen
screen也是window对象的属性之一。window.screen对象包含有关用户屏幕的信息。
Screen可用宽度
screen.availWidth属性返回访问者屏幕的宽度,以像素计,减去界面特性,比如窗口任务栏。
比如,返回您的屏幕的可用宽度:
<script>
document.write("可用宽度:" + screen.availWidth);
</script>
Screen可用高度
screen.availHeight属性返回访问者屏幕的高度,以像素计,减去界面特性,比如窗口任务栏。
比如,返回您的屏幕的可用高度:
<script>
document.write("可用高度:" + screen.availHeight);
</script>
window.location
location也是window对象的属性之一。window.location对象用于获得当前页面的地址(URL),并把浏览器重定向到新的页面。
location属性
- location.hostname 返回 web 主机的域名
- location.pathname 返回当前页面的路径和文件名
- location.port 返回 web 主机的端口 (80 或 443)
- location.protocol 返回所使用的 web 协议(http:// 或 https://)
- location.href 属性返回当前页面的 URL。
- location.pathname 属性返回URL的路径名。
- location.assign()方法加载新的文档。
window.history
history也是window对象的属性之一。为了保护用户隐私,对JavaScript访问该对象的方法做出了限制。
window.history.back()
history.back()方法加载历史列表中的前一个URL。
这与在浏览器中点击后退按钮是相同的:
<html>
<head>
<script>
function goBack()
{
window.history.back()
}
</script>
</head>
<body>
<input type="button" value="Back" onclick="goBack()">
</body>
</html>
window.history.forward()
history.forward()方法加载历史列表中的下一个URL。
这与在浏览器中点击前进按钮是相同的:
<html>
<head>
<script>
function goForward()
{
window.history.forward()
}
</script>
</head>
<body>
<input type="button" value="Forward" onclick="goForward()">
</body>
</html>
window.navigator
navigator也是window对象的属性之一。
<div id="example"></div>
<script>
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>";
document.getElementById("example").innerHTML=txt;
</script>
警告:来自navigator对象的信息具有误导性,不应该被用于检测浏览器版本,这是因为:
- navigator数据可被浏览器使用者更改
- 浏览器无法报告晚于浏览器发布的新操作系统
消息框
JavaScript中有三种消息框:警告框、确认框、提示框。
警告框 alert
语法规则为:
alert("文本")
确认框 confirm
语法规则为:
confirm("文本")
提示框 prompt
语法规则为:
prompt("文本", "默认值")
计时 Timing
通过使用JavaScript,我们有能力做到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。我们称之为计时事件。
设置定时 setTimeout()
语法规则为:
var t=setTimeout("javascript语句",毫秒)
setTimeout() 方法会返回某个值。清除定时器clearTimeout时会使用到。
清除定时器 clearTimeout()
<!DOCTYPE html>
<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()
{
clearTimeout(t)
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
</body>
</html>
JS Cookies
cookie是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个cookie。你可以使用JavaScript来创建和取回cookie的值。
cookie的例子
名字cookie
当访问者首次访问页面时,他或她也许会填写他/她们的名字。名字会存储于cookie中。当访问者再次访问网站时,他们会收到类似”Welcome John Doe!”的欢迎词。而名字则是从cookie中取回的。
密码cookie
当访问者首次访问页面时,他或她也许会填写他/她们的密码。密码也可被存储于cookie中。当他们再次访问网站时,密码就会从cookie中取回。
日期cookie
当访问者首次访问你的网站时,当前的日期可存储于cookie中。当他们再次访问网站时,他们会收到类似这样的一条消息: “Your last visit was on Tuesday August 11, 2005!”。日期也是从cookie中取回的。
创建和存储cookie (document.cookie)
<!DOCTYPE html>
<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 ""
}
<!-- 设置cookie内容,"用户名 + 超期时间" -->
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>
更多请参考:W3School