本文共 6582 字,大约阅读时间需要 21 分钟。
浏览器对象模型 (BOM) 使 JavaScript 有能力与浏览器“对话”
浏览器对象模型(Browser Object Model)尚无正式标准。
所有浏览器都支持 window 对象。它表示浏览器窗口。
所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员。
全局变量是 window 对象的属性。
全局函数是 window 对象的方法。
甚至 HTML DOM 的 document 也是 window 对象的属性之一:
window.document.getElementById("header");
document.getElementById("header"); 和上面的是相同的,window 可以不加,是默认的
有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)。
对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:
对于 Internet Explorer 8、7、6、5:
或者
实用的 JavaScript 方案(涵盖所有浏览器):
==========
window.screen 对象包含有关用户屏幕的信息
window.screen 对象在编写时可以不使用 window 这个前缀。
一些属性:
screen.availHeight 属性返回访问者屏幕的高度,以像素计,减去界面特性,比如窗口任务栏。
================
window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面。
window.location 对象在编写时可不使用 window 这个前缀。
一些例子:
/js/js_window_location.asp
location.href 属性返回当前页面的 URL。//
http://www.w3school.com.cn/js/js_window_location.asp
<head> <script> function newDoc() { window.location.assign("http://www.w3school.com.cn") } </script> </head> <body> <input type="button" value="加载新文档" οnclick="newDoc()"> </body>
================
window.history 对象包含浏览器的历史。
window.history 对象在编写时可不使用 window 这个前缀。
为了保护用户隐私,对 JavaScript 访问该对象的方法做出了限制。
history.back() 方法加载历史列表中的前一个 URL。
history forward() 方法加载历史列表中的下一个 URL。
这与在浏览器中点击前进按钮是相同的:
window.navigator 对象包含有关访问者浏览器的信息。
<body> <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> </body>
来自 navigator 对象的信息具有误导性,不应该被用于检测浏览器版本,这是因为:
由于 navigator 可误导浏览器检测,使用对象检测可用来嗅探不同的浏览器。
由于不同的浏览器支持不同的对象,您可以使用对象来检测浏览器。例如,由于只有 Opera 支持属性 "window.opera",您可以据此识别出 Opera。
例子:if (window.opera) {...some action...}
===========
可以在 JavaScript 中创建三种消息框:警告框、确认框、提示框
警告框经常用于确保用户可以得到某些信息。
当警告框出现后,用户需要点击确定按钮才能继续进行操作
警告框====alert("我是警告框!!")
带折行的警告框=========alert("再次向您问好!在这里,我们向您演示" + '\n' + "如何向警告框添加折行。")
================
确认框用于使用户可以验证或者接受某些信息。
当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作。
如果用户点击确认,那么返回值为 true。如果用户点击取消,那么返回值为 false。
确认框========var r=confirm("Press a button!");//弹出一个确认框,如果点击好,返回的结果是true,如果点击的是取消,返回的结果是false;
if (r==true) { alert("You pressed OK!"); } else { alert("You pressed Cancel!"); }
=============
提示框经常用于提示用户在进入页面前输入某个值。
当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。
如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。
prompt("文本","默认值")
<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="显示提示框" /> ==================
通过使用 JavaScript,我们有能力做到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。我们称之为计时事件。
function timedMsg() { var t=setTimeout("alert('5 秒!')",5000)//5秒钟后弹出alert的内容; } ====
<script type="text/javascript"> function timedText() { var t1=setTimeout("document.getElementById('txt').value='2 秒'",2000)//2秒钟后之执行document.getElementById('txt').value='2 秒'"这句话 var t2=setTimeout("document.getElementById('txt').value='4 秒'",4000) var t3=setTimeout("document.getElementById('txt').value='6 秒'",6000) } </script> </head> <body> <form> <input type="button" value="显示计时的文本" onClick="timedText()"> <input type="text" id="txt"> </form> <p>点击上面的按钮。输入框会显示出已经逝去的时间(2、4、6 秒)。</p> </body>
==============无限计时==========
<script type="text/javascript"> var c=0 var t function timedCount() { document.getElementById('txt').value=c c=c+1 t=setTimeout("timedCount()",1000) } </script> </head> <body> <form> <input type="button" value="开始计时!" onClick="timedCount()"> <input type="text" id="txt"> </form> <p>请点击上面的按钮。输入框会从 0 开始一直进行计时。</p> </body>
==========
var t=setTimeout("javascript语句",毫秒)setTimeout() 方法会返回某个值。在上面的语句中,值被储存在名为 t 的变量中。假如你希望取消这个 setTimeout(),你可以使用这个变量名来指定它。
=========cooie=============
cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。你可以使用 JavaScript 来创建和取回 cookie 的值。
在这个例子中我们要创建一个存储访问者名字的 cookie。当访问者首次访问网站时,他们会被要求填写姓名。名字会存储于 cookie 中。当访问者再次访问网站时,他们就会收到欢迎词。
<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> < ===================转载地址:http://azmgi.baihongyu.com/