window对象是BOM的核心,window对象指当前的浏览器窗口。
1.打开窗口
window.open(('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');//打开新窗口
window.open 弹出新窗口的命令;
page.html 弹出窗口的文件名;
newwindow 弹出窗口的名字(不是文件名),非必须,可用空”代替;
height=100 窗口高度;
width=400 窗口宽度;
top=0 窗口距离屏幕上方的象素值;
left=0 窗口距离屏幕左侧的象素值;
toolbar=no 是否显示工具栏,yes为显示;
menubar,scrollbars 表示菜单栏和滚动栏。
resizable=no 是否允许改变窗口大小,yes为允许;
location=no 是否显示地址栏,yes为允许;
status=no 是否显示状态栏内的信息(通常是文件已经打开),yes为允许;
2.计时器
setInterval(代码,交互时间);
<script type="text/javascript">
var int = setInterval(clock, 2000)
function clock() {
var time = new Date();
document.getElementById("clock").value = time;
}
</script>
<input type="text" id="clock" size="50" />
取消计时器clearInterval()
<script type="text/javascript">
function clock() {
var time=new Date();
document.getElementById("clock").value = time;
}
var i=setInterval("clock()",1000);
</script>
<input type="text" id="clock" size="50" />
<input type="button" value="Stop" onclick="clearInterval(i)" />
setTimeout(代码,延迟时间);
<script type="text/javascript">
setTimeout("alert('Hello!')", 3000 );
</script>
setTimeout()和clearTimeout()一起使用,停止计时器。
<script type="text/javascript">
var num = 0;
var i;
function startCount() {
document.getElementById('count').value = num;
num = num + 1;
i = setTimeout("startCount()", 1000);
}
function stopCount() {
clearTimeout(i);
}
</script>
<input type="button" value="Start" id="count" onClick="startCount()"/>
<input type="button" value="Stop" onClick="stopCount()"/>
3.history对象
history对象记录了用户曾经浏览过的页面(URL),并可以实现浏览器前进与后退相似导航的功能。
注意:从窗口被打开的那一刻开始记录,每个浏览器窗口、每个标签页乃至每个框架,都有自己的history对象与特定的window对象关联。
<script type="text/javascript">
var HL = window.history.length ;
document.write(HL);
function last(){
var last = window.history.back();
}
function next(){
var next = window.history.forward();
}
function go(){
var go = window.history.go("number");//number为浏览器前历史记录数值如返回上一页则为go(1);
}
</script>
4.Location对象
location用于获取或设置窗体的URL,并且可以用于解析URL。
alert(window.location.href)
5.Navigator对象
Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本。
var browser=navigator.appName;
var b_version=navigator.appVersion;
document.write(browser);
document.write("<br>");
document.write(b_version);
6.userAgent
返回用户代理头的字符串表示(就是包括浏览器版本信息等的字符串)
var u_agent = navigator.userAgent;
var B_name="Failed to identify the browser";
if(u_agent.indexOf("Firefox")>-1){
B_name="Firefox";
}else if(u_agent.indexOf("Chrome")>-1){
B_name="Chrome";
}else if(u_agent.indexOf("MSIE")>-1&&u_agent.indexOf("Trident")>-1){
B_name="IE(8-10)";
}
document.write("B_name:"+B_name+"<br>");
document.write("u_agent:"+u_agent+"<br>");
7.screen对象
screen对象用于获取用户的屏幕信息。
alert( window.screen.属性)