BOM(浏览器对象模型)——与浏览器对话:
Window对象(代表浏览器的窗口——不包括工具栏、滚动条):
//所有全局对象、全局函数,均自动成为window对象的成员(document属于浏览器,所以属于window;接下来提到的对象都属于window)
window.document.write("hello" + "<br />");
//内部尺寸
document.write(window.innerHeight + " : " + window.innerWidth + "<br />");
Screen对象:
document.write("屏幕尺寸: " + screen.availHeight + "*" + screen.availWidth + "<br />");
location对象(用于获取当前页面的url和处理其他的url):
document.write(location.pathname + "<br />"); //去掉协议和"://",还有主机名
document.write(location.href + "<br />");
document.write(location.protocol + "<br />");
location.assign("http://www.baidu.com"); //重新加载新的url(self):还有window的open — window.open("url")/_blank
获取浏览器的浏览历史——history对象:
<!--页面1-->
<button onclick="location.assign('back.html')">另一个页面</button>
<button onclick="history.forward()">下一个页面(管他下一个是什么)</button>
<!--history.go(number|URL)-->
<button onclick="history.go(1)">向下一个页面go(forward)</button>
<!--页面2-->
<button onclick="history.back()">回到刚才页面</button>
<button onclick="history.forward()">下一个页面(管他下一个是什么)</button>
<button onclick="history.go(-1)">向前一个页面go(back)</button>
获得访问者浏览器的信息——navigator对象:
但是这个对象返回的信息可能会具有误导性,不宜用来检测其浏览器版本——navigator对象的数据可被对方修改:
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.write(txt);
计时:
var t = setTimeout("alert('5秒后的弹框')", 5000); //第一个参数为时间到了之后,执行的操作
clearTimeout(1); //取消第一个,不能一次清除所有,只能指定——写成clearTimeout(t)更方便
Cookies:
cookie 是存储于访问者的计算机中的变量,由访问者访问时发送。——可在页面使用JS读取、创建cookies。常用cookies有名字、密码、日期cookie。
document.cookie = "name=" + "shutao"; //创建