浏览器对象包括 Window(窗口) 、Navigator(浏览器) 、Screen (客户端屏幕) 、History(访问历史) 、Location(浏览器地址) 。
Window(窗口)
一旦页面加载,就会自动创建window对象,所以无需手动创建window对象。通过window对象可以获取文档显示区域的高度(window.innerHeight)和宽度(window.innerWidth),获取外部窗体的宽度(window.outerWidth)和高度(window.outerHeight),还可以通过window.open( )方法打开一个新的网站。
浏览器上常见的弹出框有 警告框,确认框,提示框 这些都是通过调用window的方法实现的。
- 警告框 alert,常用于消息提示,比如注册成功等等;
- 确认框 confirm,常用于危险性操作的确认提示。 比如删除一条记录的时候,弹出确认框confirm返回基本类型的Boolean true或者false;
- 输入框 prompt,用于弹出一个输入框,供用户输入相关信息。 因为弹出的界面并不好看,很有可能和网站的风格不一致,所以很少会在实际工作中用到。
Navigator即浏览器对象,提供浏览器相关的信息
<script type="text/javascript">
document.write("<p>浏览器产品名称:");
document.write(navigator.appName + "</p>");
document.write("<p>浏览器版本号:");
document.write(navigator.appVersion + "</p>");
document.write("<p>浏览器内部代码:");
document.write(navigator.appCodeName + "</p>");
document.write("<p>操作系统:");
document.write(navigator.platform + "</p>");
document.write("<p>是否启用Cookies:");
document.write(navigator.cookieEnabled + "</p>");
document.write("<p>浏览器的用户代理报头:");
document.write(navigator.userAgent + "</p>");
</script>
Screen对象表示用户的屏幕相关信息
<script type="text/javascript">
document.write("用户的屏幕分辨率: ")
document.write(screen.width + "*" + screen.height)
document.write("<br />")
document.write("可用区域大小: ")
document.write(screen.availWidth + "*" + screen.availHeight)
document.write("<br />")
</script>
History用于记录访问历史
<script>
function goBack()
{
history.go(-2); //-1表示上次,-2表示上上次,以次类推
}
</script>
Location表示浏览器中的地址栏
function p(s){
document.write(s);
document.write("<br>");
}
p("协议 location.protocol:"+location.protocol);
p("主机名 location.hostname:"+location.hostname);
p("端口号 (默认是80,没有即表示80端口)location.port:"+location.port);
p("主机加端口号 location.host:"+location.host);
p("访问的路径 location.pathname:"+location.pathname);
p("锚点 location.hash:"+location.hash);
p("参数列表 location.search"+location.search);
除了以上的用法外,location还可以调用location.reload()方法刷新页面,利用location.assign()跳转到指定的页面。