版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/czh500/article/details/84947402
javascript中的window对象的知识点
内置对象定义:可以不需要声明和创建,可以直接使用,内置对象全部为javascript关键字
window是内置对象,因为window使用的太频繁了,所以一般window可以省略不写,我们平时用的alert()其本质就是window.alert();所以内置对象调用属性和方法的时候可以不用写对象名,而直接写方法名和属性名document.getElementById("xxx");等效于window.document.getElementById("xxx");
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>javascript中的window对象的知识点,雪豹软件工作室,javascript</title>
<link rel="stylesheet" type="text/css" href="mark.css">
<link rel="stylesheet" type="text/css" href="input2.css">
<link rel="stylesheet" type="text/css" href="body.css">
<script type="text/javascript">
/*内置对象定义:可以不需要声明和创建,可以直接使用,内置对象全部为javascript关键字*/
/*
window是内置对象,因为window使用的太频繁了,所以一般window可以省略不写,我们平时用的alert()其本质就
是window.alert();所以内置对象调用属性和方法的时候可以不用写对象名,而直接写方法名和属性名
document.getElementById("xxx");等效于window.document.getElementById("xxx");
*/
function testWindow() {
alert("test");
window.alert("hello");
var str = "123";
alert(str + 12);
alert(parseInt(str)+ 12);
/*
这里有个知识点(了解Javascript函数:parseInt(),参考https://www.2cto.com/kf/201304/203710.html或
者参考https://yq.aliyun.com/ziliao/72451 ),暂时没时间研究,以后再深究吧
*/
//parseInt()本质上是window.parseInt(),即parseInt()等效于window.parseInt()
alert(parseInt(str)+ 12 + window.parseInt("1"));
//alert("href = " + window.location.href);
//alert("location = " + window.location);
//alert("href = " + location.href);
}
function testAlert(){
window.alert("测试window.alert方法");
}
function testConfirm(){
if (window.confirm("您确定吗?")) {
alert("您选择了确定!");
}else {
alert("您选择了取消!");
}
}
function testOpen(){
//新窗口打开
//window.open("http://www.sogou.com");
//window.open("http://www.sogou.com", "_self");
//window.open("calculator.html");
//window.status = "测试哈哈"; //状态栏显示文本
/*
for (var count = 1; count <= 5; count++) {
window.open("calculator.html");
}
*/
window.open("calculator.html", "test", "toolbars=1, location=0, statusbars=0, menubars=0,width=500,height=500,scrollbars=1")
}
function testStatus(){
//window.status = "测试哈哈"; //状态栏显示文本
}
//模式窗体
function testModalDialog(){
//模式窗体(showModalDialog方法在谷歌浏览器中不支持,而且在谷歌浏览器中报错,在火狐浏览器中运行没问题,火狐浏览器支持该方法,其他浏览器没测试,就测试了火狐和谷歌)
window.showModalDialog("calculator.html");
}
function testClose(){
window.close();
}
function testScreen(){
//可以这样理解,screen也是一个内置对象,screen对象是window对象内的一个属性,window一般可以省略不写,所以可以直接写成screen.height
alert("屏幕大小 = " + window.screen.width + " " + screen.height);
if (screen.width != 1024 || screen.height != 768 ) {
alert("请把屏幕设置成" + screen.width + " " + screen.height);
}
}
function testHistory(){
//history.back();
//history.forward();
//正数 1 就是向前(前进)即forward(),负数 -1就是向后(后退)即back()
//history.go(-1);
}
function testLocation(){
location.href = "calculator.html";
//window.location.href = "calculator.html";
//window.location = "calculator.html";
//location = "calculator.html";
}
</script>
</head>
<body>
<input type="button" value="javascript代码直接写在事件中测试1" onclick="javascript:alert('测试哈哈哈')">
<input type="button" value="javascript代码直接写在事件中测试2" onclick="alert('测试呵呵呵')">
<input type="button" value="测试window对象的知识点" onclick="testWindow()">
<input type="button" value="测试window.alert方法" onclick="testAlert()">
<input type="button" value="测试window.confirm方法" onclick="testConfirm()">
<input type="button" value="测试window.open方法" onclick="testOpen()">
<input type="button" value="测试浏览器状态栏" onclick="testStatus()">
<input type="button" value="测试模式窗体" onclick="testModalDialog()">
<input type="button" value="测试关闭窗体,测试close方法" onclick="testClose()">
<input type="button" value="测试屏幕" onclick="testScreen()">
<input type="button" value="测试location" onclick="testLocation()">
</body>
</html>