JavaScript | window浏览器对象模型
Js Window - 获取浏览器窗口
- 全局变量是window对象的属性
- 全局函数是window对象的方法
- HTML DOM的document是window对象属性之一
window.document.getElementById("header"); === document.getElementById("header");
window.innerHeight window.innerWidth |
获取浏览器尺寸 IE/Chrome/Firefox/Opera/Safari |
document.documentElement.clientHeight document.documentElement.clientWidth |
获取浏览器尺寸 IE8/7/6/5 |
document.body.clientHeight document.body.clientWidth |
获取浏览器尺寸 其他 |
window.open() |
打开新窗口 |
window.close() |
关闭当前窗口 |
window.moveTo() |
移动当前窗口 |
window.resizeTo() |
调整当前窗口的尺寸 |
获取window尺寸
var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var heigh = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
console.log(width + "," + heigh);
————————————————————————————————————————————
Js Screen - 获取屏幕的信息
screen.availWidth |
获取屏幕宽度 |
screen.availHeight |
获取屏幕高度 |
————————————————————————————————————————————
Js Location - 获取页面当前位置
location.href |
返回当前链接 |
location.hostname |
返回 web 主机的域名 |
location.pathname |
返回当前页面的路径和文件名 |
location.port |
返回 web 主机的端口 (80 或 443) |
location.protocol |
返回所使用的 web 协议(http:// 或 https://) |
location.assign("http://www.xxx.cn") |
加载新的文档 |
————————————————————————————————————————————
Js History - 获取浏览器历史
history.forward(); |
前进 |
history.back(); |
后退 |
————————————————————————————————————————————
Js Navigator - 访问者浏览器的信息
p.s.来自 navigator 对象的信息具有误导性,不应该被用于检测浏览器版本,这是因为:navigator 数据可被浏览器使用者更改,浏览器无法报告晚于浏览器发布的新操作系统。
使用对象检测可用来嗅探不同的浏览器。由于不同的浏览器支持不同的对象,您可以使用对象来检测浏览器。例如,由于只有 Opera 支持属性 "window.opera",您可以据此识别出 Opera。
navigator.appCodeName |
浏览器内核 |
navigator.appName |
浏览器名称 |
navigator.appVersion |
内核版本 |
navigator.cookieEnabled |
Cookie是否开启 |
navigator.platform |
系统平台 |
navigator.userAgent |
浏览器代理 |
navigator.systemLanguage |
语言 |
————————————————————————————————————————————
Js PopupAlert - 消息框
alert("文本") |
警告框 |
confirm("文本") |
确认框 |
prompt("文本","默认值") |
提示框 |
提示框样例
var name = prompt("please input your name:", "hugh dong")
if (name != null && name != "") {
document.write("hello," + name);
}
————————————————————————————————————————————
Js Timing - 计时事件
setTimeout() |
未来的某时执行代码 |
clearTimeout() |
取消setTimeout() |
时钟样例
<!DOCTYPE html>
<html> <head>
<title></title>
<!-- <script type="text/javascript" src="test.js"></script> -->
</head> <body>
<div>
<p id="txt"></p>
<input type="button" value="stop" onclick="stop()">
</div>
<script type="text/javascript">
// 调用timeOut()5秒后弹出alert
function timeOut() {
var t1 = setTimeout("alert('5 second')", 5000);
}
// timeOut();
// *********************************************************************
// 秒表计时,控制台每秒输出秒数
var c = 0; function timedCount() {
console.log(c);
c = c + 1
t2 = setTimeout("timedCount()", 1000)
}
timedCount();
// *********************************************************************
// 简单时钟
function startTime() {
var today = new Date()
var h = today.getHours()
var m = today.getMinutes()
var s = today.getSeconds()
m = checkTime(m)
s = checkTime(s)
document.getElementById('txt').innerHTML = h + ":" + m + ":" + s;
t3 = setTimeout('startTime()', 500)
} function checkTime(i) {
if (i < 10) { i = "0" + i }
return i
}
startTime();
// *********************************************************************
// 停止时钟
function stop() {
clearTimeout(t3);
}
</script>
</body> </html>
————————————————————————————————————————————
Js Cookies
- 名字 cookie
- 密码 cookie
- 日期 cookie
Cookie创建样例
<!DOCTYPE html>
<html> <head>
<title></title>
<script type="text/javascript" src="test.js"></script>
</head> <body onload="checkCookie()">
</body> </html>
// 获取cookie
function getCookie(c_name) {
if (document.cookie.length > 0) {
// 检索cookie中的内容
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 "";
} // 创建cookie,输入姓名,值,密码
function setCookie(c_name, value, expiredays) {
// 获取当前时间
var exdate = new Date();
// 根据当前时间的'天'+过期天数,建立新的天数(秒单位)
exdate.setDate(exdate.getDate() + expiredays);
// 创建cookie内容
document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
} function checkCookie() {
username = getCookie('username'); // 获取cookie
// 如果cookie存在则弹窗欢迎
if (username != null && username != "") {
alert('Welcome again ' + username + '!');
}
// cookie不存在则创建cookie
else {
// 弹窗输入用户名
username = prompt('Please enter your name:', "");
// 如果用户名不为空则创建cookie
if (username != null && username != "") {
setCookie('username', username, 365);
}
}
}
最新文章
- CGGeometry.h 文件详解
- 二分图最大匹配算法-Hopcroft-Karp模板
- 【POJ】2187 Beauty Contest(旋转卡壳)
- Asp.Net 之 枚举类型的下拉列表绑定
- C#多线程的介绍(园子里比较全的一篇)
- [jobdu]扑克牌顺子
- 嵌入式Linux-GNU Make 使用手册(中译版)
- datagrid合并行列--并不能影响序号列内容...(formatter的锅.)
- Windows Azure案例分析: 选择虚拟机或云服务?
- 理解FTP协议
- C++中实现 time_t, tm 相互转换
- hdu 1253 胜利大逃亡(BFS)
- 360度角转AS3角度
- UITextField 设置 placeholder 的字体颜色方法
- Linux启动kettle及linux和windows中kettle往hdfs中写数据(3)
- 表格和echart二级联动,并通过点击echart高亮图标单元格
- [转载] Spark:大数据的“电光石火”
- raid5 阵列硬盘离线数据恢复成功案例
- mybatis自动生成
- 预装WIN8的电脑是GPT分区模式,无法安装WIN7
热门文章
- IN 运算符
- VB查询数据库之组合查询——机房收费总结(二)
- 【2-sat】Gym - 101201F - Illumination
- 【找规律】【递推】【二项式定理】Codeforces Round #419 (Div. 1) B. Karen and Test
- 【动态规划】CDOJ1651 Uestc的命运之旅
- python基础之递归,匿名,内置函数
- mac 下做csv文件的读取与生成的 遇到的坑
- Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分
- iOS10 App跳转到系统设置
- mysql错误 Access denied for user &#39;root&#39;@&#39;xxx.xxx.xxx.xxx&#39; (using password: YES)