Javascript学习笔记(十三)--window浏览器对象模型

功能:使 JavaScript 有能力与浏览器“对话”

所有浏览器都支持 window 对象。它表示浏览器窗口。
所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员。
全局变量是 window 对象的属性。
全局函数是 window 对象的方法。
甚至 HTML DOM 的 document 也是 window 对象的属性之一:
window.document.getElementById("header");
与此相同:
document.getElementById("header");

其他window方法:
window.open() - 打开新窗口
window.close() - 关闭当前窗口
window.moveTo() - 移动当前窗口
window.resizeTo() - 调整当前窗口的尺寸

1. window screem

window.screen 对象包含有关用户屏幕的信息。
window.screen 对象在编写时可以不使用 window 这个前缀。
一些属性:
screen.availWidth - 可用的屏幕宽度
screen.availHeight - 可用的屏幕高度

2. window location

一些属性
location.href 属性返回当前页面的 URL。
结果输出为:http://www.w3school.com.cn/js/js_window_location.asp

location.hostname 返回 web 主机的域名

location.pathname 返回当前页面的路径和文件名
结果输出为:/js/js_window_location.asp

location.port 返回 web 主机的端口 (80 或 443)

location.protocol 返回所使用的 web 协议(http:// 或 https://)

location.assign() 方法加载新的文档。

3. windows history

history.back() - 与在浏览器点击后退按钮相同
history.forward() - 与在浏览器中点击按钮向前相同

4. Window Navigator

window.navigator 对象包含有关访问者浏览器的信息。
例如:

<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>

5. 消息框

可以在 JavaScript 中创建三种消息框:警告框、确认框、提示框。

  1. 警告框alert
    当警告框出现后,用户需要点击确定按钮才能继续进行操作。
    alert("text")
  2. 确认框confirm
    确认框用于使用户可以验证或者接受某些信息。
    当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作。
    如果用户点击确认,那么返回值为 true。如果用户点击取消,那么返回值为 false。
    confirm("文本")
  3. 提示框
    提示框经常用于提示用户在进入页面前输入某个值。
    当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。
    如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。
    prompt("文本","默认值")

6. 计时

通过使用 JavaScript,我们有能力做到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。我们称之为计时事件。

  1. setTimeout()
    语法
    var t=setTimeout("javascript语句",毫秒)
    例1:
<head>
<script type="text/javascript">
function timedMsg()
 {
 var t=setTimeout("alert('5 seconds!')",5000)
 }
</script>
</head>
<body>
<form>
<input type="button" value="Display timed alertbox!" onClick="timedMsg()">
</form>
</body>

例2:无限循环

<head>
<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="Start count!" onClick="timedCount()">
<input type="text" id="txt">
</form>
</body>
  1. clearTimeout()
    取消setTimeout()
    语法:clearTimeout(setTimeout_variable)

7. cookies

作用:用来识别客户
什么是cookie?
cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。你可以使用 JavaScript 来创建和取回 cookie 的值。

  1. 创建一个可在 cookie 变量中存储访问者姓名的函数:
  2. 创建另一个函数来检查是否已设置 cookie:
  3. 如果 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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容