1.window
1.常用的提示框
window.alert(“弹框”);
和
if(window.confirm("确认是否提交?"))
{
console.log("确认");
}
else{
console.log("用户拒绝");
};
返回值为字符串 : var price = prompt("请输入价格:", '0');
2. window的事件
window.onload = function () {
console.log("加载完成");
}
3.窗口大小变化事件
window.onresize = function () {
//浏览器可视区域的尺寸
console.log(window.innerWidth,window.innerHeight);
//浏览器的大小
console.log(window.outerWidth,window.outerHeight);
//屏幕的实际尺寸
console.log(window.screen.width);
console.log(window.screen.height);
//屏幕尺寸 不包含任务栏
console.log(window.screen.availWidth);
console.log(window.screen.availHeight);
console.log(document.documentElement.clientWidth);
console.log(window.screen.availWidth);
console.log(window.innerWidth);
}
4.禁止浏览器的右键菜单
window.oncontextmenu = function (e) {
//阻止事件默认行为
e.preventDefault();
}
5.加载失败
window.onerror=function (){
console.log(1);
}
6.navigator 对象 存在当前设备信息系统环境
可以使用下边这个代码检测 设备系统
console.log(window.navigator.userAgent);
7.浏览器上的复制事件 (给body添加)
<style>
.copy{
opacity: 0;
}
</style>
<script>
document.body.oncopy = function (e) {
//根据浏览器的兼容写代码
//document.selection.createRange().text 这个是ie 识别
var selection = window.getSelection() ? window.getSelection() : document.selection.createRange().text;
var link = "<br/>******************************************************<br/>" + +
"转载来源:<br/>" +
"原文地址:" + location.href +
"<br/>******************************************************<br/>";
var copytext = selection + link;
if (window.clipboardData) {
window.clipboardData.setData("Text", copytext);
} else {
//使用元素创建
var div=document.createElement("div");
div.className="copy";
div.innerHTML=copytext;
document.body.appendChild(div);
selection.selectAllChildren(div);
setTimeout(function (){
div.remove();
},0)
}
}
</script>
2.history
- history.back();//后退
history.forward();//前进
history.go();//参数: 1 前进1 -1倒退1
3.location
1.地址栏对象
console.log(location);
console.log(location.host);//设置或返回当前 URL 的主机名称和端口号。
console.log(location.hostname);//设置或返回当前 URL 的主机名
console.log(location.origin);
console.log(location.pathname);//设置或返回当前 URL 的路径部分
console.log(location.port);//设置或返回当前 URL 的端口部分
2.重新加载: location.reload();
3.获取地址: console.log(location.href);
4.跳转页面
location.href=“https://www.baidu.com”;
location.replace(“https://www.baidu.com”);