问题?javascript之js window详解77d226c7a2080f1d99ea5951bd228e92
定义:javascript window - 浏览器对象模型
浏览器对象模型 (bom)
浏览器对象模型(browser object model)尚无正式标准。
由于现代浏览器已经(几乎)实现了 javascript 交互性方面的相同方法和属性,因此常被认为是 bom 的方法和属性。
1.window 对象
所有浏览器都支持 window 对象。它表示浏览器窗口。
所有 javascript 全局对象、函数以及变量均自动成为 window 对象的成员。
全局变量是 window 对象的属性。
全局函数是 window 对象的方法。
甚至 html dom 的 document 也是 window 对象的属性之一:
window.document.getelementbyid("header");
与此相同:
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
或者
这个是得到body的长宽
document.body.clientheight
document.body.clientwidth
如:
var w = window.innerwidth;
var h = window.innerheight;
/*这个获取的长宽与下列的相同*/
var w2 = document.documentelement.clientwidth;
var h2 = document.documentelement.clientheight;
/*这得到的是body的长度*/
var h1 = document.body.clientheight;
var w1 = document.body.clientwidth;
document.getelementbyid("a1").innerhtml="内部窗口大小为:"+w+"*"+h+" "+w1+"*"+h1+" "+w2+"*"+h2;
3.其他 window 方法
一些其他方法:
window.open() - 打开新窗口
如:
window.open('1.html','ss');
window.close() - 关闭当前窗口window.moveto() - 移动当前窗口window.resizeto() - 调整当前窗口的尺寸
如:
window.resizeto(600,600);
4.localtion
window.location 对象用于获得当前页面的地址 (url),并把浏览器重定向到新的页面。
window location
window.location 对象在编写时可不使用 window 这个前缀。
一些例子:
location.hostname 返回 web 主机的域名
location.pathname 返回当前页面的路径和文件名
location.port 返回 web 主机的端口 (80 或 443)
location.protocol 返回所使用的 web 协议(http:// 或 https://)
例子:
document.getelementbyid("a2").innerhtml="主机名"+location.hostname+"
路径:"+location.pathname+" 端口号:"+location.port+" web协议:"+location.protocol;
例子:
document.getelementbyid("a2").innerhtml="主机名"+location.hostname+"
路径:"+location.pathname+" 端口号:"+location.port+" web协议:"+location.protocol+" irl:"+location.href;
5.window location assign
location.assign() 方法加载新的文档。
例子:
window.location.assign("http://www.w3school.com.cn");
window.location.assign("1.html");
6.window history
window.history 对象在编写时可不使用 window 这个前缀。
为了保护用户隐私,对 javascript 访问该对象的方法做出了限制。
一些方法:
history.back() - 与在浏览器点击后退按钮相同
history.forward() - 与在浏览器中点击按钮向前相同
7.window.navigator 对象包含有关访问者浏览器的信息。
window navigator
window.navigator 对象在编写时可不使用 window 这个前缀。
实例
<p id="example"></p>
<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;
警告:来自 navigator 对象的信息具有误导性,不应该被用于检测浏览器版本,这是因为:
navigator 数据可被浏览器使用者更改
浏览器无法报告晚于浏览器发布的新操作系统
8.消息框:
警告框: balert("再次向您问好!在这里,我们向您演示" + '\n' + "如何向警告框添加折行。")
确认框: var r=confirm("press a button!") 这里的r值,点击确认为true,否则为false
提示框: var name=prompt("请输入您的名字","bill gates")前者是提示信息,后者是默认值,name是输入框的返回值
二、javascript 计时
通过使用 javascript,我们有能力做到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。我们称之为计时事件。
javascript 计时事件
通过使用 javascript,我们有能力作到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。我们称之为计时事件。
在 javascritp 中使用计时事件是很容易的,两个关键方法是:
settimeout()
未来的某时执行代码
cleartimeout()
取消settimeout()
语法:var t=settimeout("javascript语句",毫秒)
settimeout("alert('5seconds message!')",5000);//5秒后执行
三、cookie
什么是cookie?
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 中取回的。
<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>
以上就是javascript之js window详解77d226c7a2080f1d99ea5951bd228e92 的内容。