js 的 bom和window对象

本文深入浅出地介绍了JavaScript的基础组成部分ECMAScript,详细讲解了BOM(浏览器对象模型)中的screen、location、navigator和history,以及window对象的全局变量、事件处理和编码转换。特别关注了window对象的open方法和常见编码实践。
摘要由CSDN通过智能技术生成

1.js的组成

ECMAScript语法
Bom 浏览器与js交互
Dom html内容与js交互

2.bom对象

(1)screen 屏幕 

document.write(screen.width)  //屏幕的宽
document.write(screen.height) //屏幕的高


document.write(screen.availWidth) //移除状态的屏幕宽
document.write(screen.availHeight)//移除状态 的屏幕高


//可视屏幕宽高
document.write(window.innerHeight)
document.write(window.innerWidth)

(2)location 地址
 

location.reload()	//普通刷新
location.reload(true)	//强制刷新,更新缓存
location.href 		//地址信息
location.protocol	//协议
location.hostname	//域名
location.pathname	//路径
location.port		//端口号
location.search		//查询
location.hash		//哈希(锚点)(?后面的值)

(3)navigator 浏览器对象

ua=navigator.userAgent;//浏览器的头信息

if(ua.includes('mobile')){
    判断是否为手机浏览器
}//判断浏览器

(4)history 历史记录

forward //前进
back	//返回
go()  //前进或后退1为前,-1为后


3.open方法

win=window.open(url,name,feature)
//url 打开文件的地址 https://www.baidu.com
//name 名称
//feature  特性  'width=400,height=400,top=200,left=200'


win.close()  //关闭打开的窗口

4.window全局变量

1.scipt中var声明变量
没有用var声明的变量 都是window全局变量

2.有id命名的html也是全局对象
可以通过window.id名访问

3.大部分都省略  window
 

alert		//警告
prompt		//确认
confirm		//弹输入框

var 声明的全局变量   可以通过window访问
id命名的元素可用  innerText(元素的文字内容)  直接访问
定时器也是全局方法  setInterval     间隔定时器,
setTimeout    延迟调用.

5.事件
onload

onscroll

onresize

<script type="text/javascript">
    //onload事件    窗口,图片加载完毕触发
    window.onload = function(){	//当窗口加载完毕执行这个function
    alert(myh.innerText);
}   //可在任意位置写入
			

    //onscroll     //滚动时候触发
    window.onscroll= function(){

	//当滚动条滚动执行这个function
	console.log('页面滚动了');

	var t = document.documentElement.scrollTop;
    //document.documentElement.scrollTop//页面垂直滚动的距离,
    //document文档 documentElement根节点
	//scrollTop垂直滚动距离,scrollLeft 水平滚动距离

	console.log(t);			
}


    // 当窗口发生大小变化时候,向页面写入窗口可视区域的宽高
	window.onresize = function(){
	console.log("窗口的大小发生了变化");
	console.log("宽:"+window.innerWidth+",高:"+window.innerHeight)
	console.log("宽:1920,高:1080")
 }
				
</script>


6.编码
 

//常见编码

utf-8	              //万国码
gb2312	              //国标
gbk	                  //中文包含繁体字

//编译为URL
decode                //转为万国码
encode                //解为万国码

    
decodeURL             //转url编码
encodeURL             //解url编码
decodeURLComponent    //转url编码,包含标点符号
encodeURLComponent    //解url编码,包含标点符号


//预览图片

img.src = window.URL.createObjectURL(my.files[0]);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值