Python学习笔记:5.2.6 javascript之BOM对象

本文是学习陆老师的《python全栈工程师 - web开发前端基础》课程的笔记,欢迎学习交流。同时感谢陆老师的精彩传授!

一、课程目标
  • window对象
  • location对象
  • navigation对象
  • history对象
二、详情解读
2.0.0.面向对象编程练习

练习1:
定义一个车的构造函数,然后再定义汽车,自行车的构造函数,汽车与自行车主要是动力不同,
轮子数量不 一样

function Vehicle(){

}
Vehicle.prototype.move = function(){
		console.log("move speed:", this.speed)
	}
	
function Car(power) {
	Car.prototype.constructor.call(this)
	this.wheel = 4
	this.speed = 100
}

Car.prototype = new Vehicle()

function Bike(power){
	Car.prototype.constructor.call(this)
	this.wheel = 2
	this.speed = 20
}
Bike.prototype = new Vehicle()

var car = new Car()
var bicycle = new Bike()

在这里插入图片描述

2.0.1.BOM - 浏览器对象模型

BOM - browser object model
包含window、location、navigator等对象

2.1.window对象

1.iframe在一个页面创建多个窗口
2.利用window.open创建子窗口
3.窗口对话框
4.url与location

测试窗口:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			a = 10
			b = 11
			// 打开当前窗口的窗口
			function hello(){
				console.log("hello world")
			}
			// document.write(window.top.name)
			
			// window.opener.console.log("opener:",window.opener)
			if (window.opener!=null){
				window.document.write("您是我的父窗口")
			}			
			
		</script>
	</body>
</html>
2.1.1.iframe

1.通过窗口名称可以调用同域名下的其他窗口变量
2.通过window.子窗口名称.属性可以访问iframe窗口的属性
3.在iframe窗口内通过window.top访问主窗口

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<!-- 
		<iframe name="no1" 
		src="http://www.sina.com.cn" width="100%" height=200 
		></iframe>
		<iframe name="no2"
		src="http://www.baidu.com" width="100%" height=200 
		></iframe>
		 -->
		<!-- <a href="http://www.baidu.com" target="no2">打开百度</a> -->
		
		<iframe name="no2" 
		src="06.1.0测试窗口.html" width="100%" height=500 
		></iframe>
        
		<script type="text/javascript">
			// iframe 是窗口框架元素,可以通过iframe在一个主窗口内嵌多个子窗口
			
			//可以通过窗口名称调用同域名下的其他窗口变量
			// no2.a
			// no2.b
			
			/* 
			可以通过window.子窗口名称.属性访问iframe窗口的属性
			比如通过location指定窗口打开页面
			window.no2.location = url
			 */
			// window.no2.location = "http://www.baidu.com"
			
			// 在iframe窗口内通过window.top访问主窗口
			window.name = "luxp"		
		</script>
	</body>
</html>
2.1.2.window.open

window.open(url, name, args):在指定名为name的窗口中打开url网址
args是一个字符串,设置新窗口的属性,多个属性以逗号隔开:

属性说明可选值
height、width窗口宽高不能小于100
top、left窗口位置
location是否显示地址栏yes 、no
menubar是否显示菜单栏yes 、no
resizable是否允许拖动浏览器窗口改变大小yes 、no
scrollbars是否允许滚动显示yes 、no
status是否允许显示状态栏yes 、no
toolbar是否允许显示工具栏yes 、no

window方法:

方法名说明
window.moveTo(x,y)将窗口移动到x,y位置
window.moveBy(offsetX, offsetY)将窗口相对原来的位置移动offsetX, offsetY距离
window.resizeTo(w,h)调整窗口大小到w, h
window.resizeBy(deltaW, deltaH)在原来大小基础上调整窗口deltaX, deltaY
window.close()关闭窗口

在A窗口中打开B窗口,A窗口可以通过window.open的返回值,比如下面示例代码中的sub_window来访问B窗口的变量属性
B窗口则通过window.opener来获取A窗口的变量属性,比如上文中测试窗口中的示例代码

示例代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>	
		<a href="http://www.baidu.com" target="newwin">打开百度</a>        
		<script type="text/javascript">			
			// 通过sub_window访问子窗口变量属性
			// 使用该方法打开窗口时,需要注意浏览器是否禁止打开子窗口
			sub_window = window.open("06.1.0测试窗口.html", "newwin", "height=300, width=300, top=0, left=0")
			sub_window.document.write("hello world")		
		</script>
	</body>
</html>
2.1.3.location属性
方法名说明
location.replace(url)改变位置但是不会留下历史记录,用户不能返回到前一个页面
location.reload()从浏览器内存中重载当前页面
location.reload(true)从服务器上重新加载当前url

window.location的组成:

组成说明举例
host/hostname服务器名,可以使用ip, 域名,主机名。host不包含端口号,hostname包含端口号127.0.0.1/127.0.0.1:8848
href表示 url 全部内容链接http://127.0.0.1:8848/javascipt/06_BOM/01_location.html?q=1&p=2&l=3#name1
pathname表示 端口号后面的内容/javascipt基础/06_BOM对象模型/06.1.3.location属性.html
port端口号比如下文中的 8848,如果是80端口可以省略
protocol请求协议http/https
origin相当于 protocol + hostnamehttp://127.0.0.1:8848
searchurl中?后面的键值对部分比如下文中的?q=1&p=2&l=3
hashurl中的锚点值比如下面的#name1
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.container{
				height: 1000px;
				border: 1px solid #f00;
			}
		</style>
	</head>
	<a href="#name1">name1</a>
	<div class="container">
		
	</div>
	<!-- 站内锚点 -->
	<a name="name1" />
	<p>想访问这里</p>
	<body>
		<script type="text/javascript">
			// http://127.0.0.1:8848/javascipt基础/06_BOM对象模型/06.1.3.location属性.html
			// http://127.0.0.1:8848/javascipt基础/06_BOM对象模型/06.1.3.location属性.html?q=1&p=2&l=3#name1
			url = window.location		   
			for (i in url){
				console.log(i,"=>", url[i])
			}			
		</script>
	</body>
</html>
2.1.4.窗口对话框
方法名说明
alert(message)警告对话框,显示message,如果message是字符串以外类型,先转换为sring类型
confirm(question)询问对话框,显示question,同时提供OK与Cancel按钮,用户点击OK,返回true,点击Cancle返回false
prompt(question, default)提示框, 显示question,与一个文本框,可以提供一个default输入,用户点击OK,返回输入值,否则返回null

示例代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<a href="" onclick='javascript:
								if (confirm("您确定要删除吗?")){
									alert("删除")
								}'>删除</a>
			
			<a href="" onclick="javascript:
			                     if (prompt('请输入您的年龄', 17)<18){
									 alert('18岁以下需要家长陪同观看')
									 return false
								 } else {
									 alert('您是成年人了,自己把控')
								 } 
								 ">18岁以下禁止观看</a>	
								
		<script type="text/javascript">
		   
		   // alert([1,2,3,45,6])
		    
			function test(){
				a=0
				while (a<10){
					alert("您真的要走么")
					a++
				}				
			}			
			
		    // 用户点击cancel, 返回false, 确定返回true
		    if (confirm("您确定要关闭吗?")){
			 	  alert("关闭")
			}
			
			function test(){
				a = 0
				if (confirm("您确定要关闭吗?")&& a<5){
					  test()
					  a++
				}
			}		   
		   
		  age = prompt("您多大啦", 18)
		  console.log(age)
		  
		</script>
	</body>
</html>

2.2.定时器

setTimeout(func, delay):
延迟delay毫秒后执行func,func可以是字符串也可以是函数该方法会返回一个ID,代表该定时器对象
延迟delay后,不代表立即执行func,需要等待队列中其他任务完成(浏览器是单线程运行javascript)
clearTimeout(ID):取消该定时器

function out(){
	console.log("hello world")
}
//第一个参数是超时后执行的函数,第二个参数是延迟多少毫秒
setTimeout(out, 5000)

//setTimeout可以返回的定时器id,该id可以用于取消定时器
timeId = setTimeout(out, 10000)
// clearTimeout 取消超时定时器

setTimeout(function(){
	clearTimeout(timeId)
}, 4000)

超时间隔定时器时间等待函数执行完所有代码后再执行定时器
setInterval指定间隔时间执行,而不会等待函数执行状态

// 间隔定时器是从上一个
function out(){
	console.log("ok")
}

setInterval(out, 1000)
timeId = setInterval(out, 1000)

// 利用setTimeout也可以实现间隔定时器
//clearInterval取消间隔定时器
setTimeout(function(){clearInterval(timeId)}, 5000)

function out(){
	console.log("hello world");
	setTimeout(out, 1000);
}
setTimeout(out, 1000);
2.3.navigator对象

通过navigator对象可以识别浏览器客户端相关信息
通过搜集navigator信息可以获知网站用户的上网信息,比如浏览器版本,系统平台,使用语言,浏览器插件等等

navigator对象提供了用户浏览器的代理信息,包含了用户的系统信息,浏览器信息。
通常用来判定客户端类型,比如pc还是移动,window还是mac
其中的userAgent通常都是爬虫需要设置的头信息

for (i in navigator){
	console.log(i,"=>", navigator[i])
}

function isPc() {
  var userAgentInfo = navigator.userAgent;
  var Agents = ["Android", "iPhone",
		"SymbianOS", "Windows Phone",
		"iPad", "iPod"];
  var flag = true;
  for (var v = 0; v < Agents.length; v++) {
	if (userAgentInfo.indexOf(Agents[v]) > 0) {
	  flag = false;
	  break;
	}
  }
  return flag;
}
2.4.history对象

history对象记录着用户的历史访问记录,但是开发人员无法获得具体的历史记录信息
可以实现前进与后退控制:

方法说明
history.go(n)当n为正,历史记录向前n的记录,为负,历史记录向后n的记录
history.go(-1)后退一页
history.go(1)前进一页
history.go(url)跳到最近的包含url的历史记录
history.back()后退
history.forward()前进
三、课程小结
  • window对象
  • location对象
  • navigation对象
  • history对象
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.sitech.prom.channelsettle.rehearsal.busi.compute.vo.DatasyncJobIdDatasyncStatusVo at com.sitech.prom.channelsettle.rehearsal.busi.compute.scheduler.TimeScanRemoteScheduler.fiveMinExecute(TimeScanRemoteScheduler.java:149) ~[classes!/:1.0] at sun.reflect.GeneratedMethodAccessor255.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161] at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.2.6.RELEASE.jar!/:5.2.6.RELEASE] at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.6.RELEASE.jar!/:5.2.6.RELEASE] at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) [spring-context-5.2.6.RELEASE.jar!/:5.2.6.RELEASE] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_161] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_161] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_161] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_161] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_161] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_161] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_161]
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值