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.事件
onloadonscroll
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]);