web/javascript内置对象
javascript有哪些内置对象
Javascript有四大内置对象
window
document
history
location
各个内置对象功能及其应用
window
出现提示框
- window.alert(“内容”):消息框
- window.confirm(“内容”):确认框
- window.prompt(“内容”):输入框
<head>
<script type="text/javascript">
function del()
{
if(window.confirm("确认删除吗")==true)
window.alert("删除成功");
else
window.alert("删除失败");
}
</script>
</head>
<body>
<table>
<tr>
<td>学号</td>
<td>姓名</td>
<td>操作</td>
</tr>
<tr>
<td>2222</td>
<td>张三</td>
<td><a href="javascript:del();">删除</a></td>
</tr>
</table>
</body>
打开关闭窗口
-window.open(“窗口地址”,“窗口名称”,“窗口状态”);
<head>
<script type="text/javascript">
window.status="出现新窗口";
window.open("window.jsp","new","width=500,height=300");
</script>
</head>
窗口状态还有很多内容,就不一一列举了
关闭打开的窗口,调用Close();
newwindow=window.open("window.jsp","new");
newwindow.close();
计时器
让某一函数隔一段时间,运行一次
<head>
<script type="text/javascript">
timer=window.setTimeout("fun1()", "1000");//要运行的函数名称,间隔时间(ms)
var arr=0;
function fun1(){
arr++;
window.status=arr;//监测到状态栏
if(arr>=5){
window.clearTimeout(timer);//清除定时器
return;//跳出循环
}
window.setTimeout("fun1()", "1000");
}
</script>
</head>
document
-输出:document.writeln();
-网页设计
-访问文档元素
<head>
<script type="text/javascript">
//document.writeln("这是网页");//显示内容
//window.alert(document.location);//获取网页网址
//window.alert(document.title);
function validate(){
var user=document.form1.user.value;
var pass=document.form1.pass.value;
if(user==""){
window.alert("用户名不可为空");
document.form1.user.focus();//使文本框获取焦点
return;
}
else if(pass==""){
window.alert("密码不可为空");
document.form1.pass.focus();
return;
}
document.form1.submit();
}
</script>
</head>
<body>
<form name="form1">
用户名:<input type="text" name="user"><br>
密 码:<input type="password" name="pass"><br>
<input type="button" value="登录" onclick="javascript:validate()">
</form>
</body>
history
代替后退及前进按钮访问历史记录
<body>
<a href="history.jsp">我是A页面</a>
</body>
<body>
<a onclick="history.back()">后退</a>
<a onclick="history.forward()">前进</a>
</body>
在A页面超链接到History,再调用back回到A
location
访问浏览器的地址
<head>
<script type="text/javascript">
function test(){
location.href="document.jsp";
}
</script>
</head>
<body>
<input type="button" value="按钮" onclick="javascript:test()">
<a href="document.jsp">超链接</a>
</body>
与超链接效果类似