1.0.6 js window对象(window,location,screen,history,popupAlert,timing,cookie)

2024-04-10 20:48

本文主要是介绍1.0.6 js window对象(window,location,screen,history,popupAlert,timing,cookie),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

<!DOCTYPE html>
<html encoding="gbk">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"></meta>
<script>
//js widnow对象
function windowMethod(){
//该例显示浏览器窗口的高度和宽度:(不包括工具栏/滚动条)
var w=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var h=window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
alert('屏幕的宽:' + w + ' 屏幕的高:' + h );
var str= 'window.innerWidth=' + window.innerWidth + ' ; window.innerHeight='+ window.innerHeight + '<br />' +
'document.documentElement.clientWidth=' + document.documentElement.clientWidth + ' ; document.documentElement.clientHeight='+ document.documentElement.clientHeight + '<br />' +
'document.body.clientWidth=' + document.body.clientWidth + ' ; document.body.clientHeight='+ document.body.clientHeight + '<br />' ;

//document.getElementById('div1').innerHTML=str;
}


//js screen
function screenMethod(){
/*
screen.availHeight 属性返回访问者屏幕的高度,以像素计,减去界面特性,比如窗口任务栏。
screen.availWidth 属性返回访问者屏幕的宽度,以像素计,减去界面特性,比如窗口任务栏。
返回屏幕的可用宽度和高度(注意:可用宽度和可用高度不会随着窗口的变化而变化,即该值是固定的)
*/
alert( '屏幕的可用宽度:'+screen.availWidth + ' --屏幕的可用高度:' + screen.availHeight);

}

//JS Location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面。

function locationMethod(){
//返回 web 主机的域名
var hostName=location.hostname; //
//返回当前页面的路径和文件名
var pathName=location.pathname;
//返回 web 主机的端口
var port=location.port;
//返回所使用的 web 协议(http:// 或 https://)
var protocol=location.protocol;
//当前页面的 URL
var url=location.href;

/*
alert(hostName + '\n' +
pathName+ '\n' +
port+ '\n' +
protocol+ '\n' +
url+ '\n' );

*/
//通过assign()方法加载新的文档
//location.assign("http://www.w3school.com.cn");
location.assign("demo61.html");
}


//JS History 对象包含浏览器的历史。
function historyMethod(){
//后退按钮 history.back() 方法加载历史列表中的前一个 URL。
//window.history.back();

//前进按钮 history forward() 方法加载历史列表中的下一个 URL
window.history.forward();

}

//window.navigator 对象包含有关访问者浏览器的信息。
function navigatorMethod(){
var codeName=navigator.appCodeName;
var appName=navigator.appName;
var cookieEnabled=navigator.cookieEnabled;
var platform=navigator.platform;
var userAgent=navigator.userAgent;
var language=navigator.systemLanguage;

alert('codeName='+ codeName +'\n' +
'appName='+ appName +'\n' +
'cookieEnabled='+ cookieEnabled +'\n' +
'platform='+ platform +'\n' +
'userAgent='+ userAgent +'\n' +
'language='+ language);

}


//可创建三种消息框: 警告框,确认框,提示框

function popupAlert(){
//警告框用 alert()
//alert("警告你一下警告你一下警告你一下警告你一下警告你一下警告你一下警告你一下警告你一下警告你一下警告你一下警告你一下警告你一下警告你一下警告你一下警告你一下警告你一下警告你一下");

//确认框
/*
var r=confirm("Plaer a button!");
if(r ==true){
alert("You pressed OK!");
}else{
alert("You pressed Cancel!");
}
*/

//提示框
//语法 prompt("文本","默认值")
var name=prompt("请输入你的名字:" , "Bill Gates");
if(name !=null && name !=""){
alert("你输入的文字是: "+ name);
}else{
alert("请输入文字!");
}

}

//计时
function timing1(){
var t=setTimeout("alert('5秒后弹出!')",5000);
}

var c=0;
var t;

//开始计时
function startTime(){
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout('startTime()',1000);
}

//停止计时
function endTime(){
clearTimeout(t);
}


//JS Cookies
function cookieMethod(){
var username=getCookie("username");
if(username !=null && username !=""){
alert("Welcome again "+ username);
}else{
username=prompt("Please enter your name:", "");
if(username !=null && username !=""){
setCookie('username',username,365);
}
}

}

//设置cookie

function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate() + expiredays);
//document.cookie=escape(c_name) + "=" + escape(value) + ";expires="+exdate.toGMTString());
document.cookie = escape(c_name) + '=' + escape(value) +';expires=' + exdate.toGMTString();
}


//获取cookie

function getCookie(c_name){
var showAllCookie = '';
if(!document.cookie == ''){
var arrCookie = document.cookie.split('; ');
//用spilt('; ')切割所有cookie保存在数组arrCookie中
document.getElementById('div1').innerHTML='arrCookie='+arrCookie + ' Cookie='+document.cookie + ' arrCookie.length='+arrCookie.length;
var arrLength = arrCookie.length;
for(var i=0; i<arrLength; i++) {
showAllCookie += 'name:' + unescape(arrCookie[i].split('=')[0]) + ' value:' + unescape(arrCookie[i].split('=')[1]) + '<br>';
}
}
return showAllCookie;

}


//删除Cookie
function deleCookieMethod(){
if(document.cookie != '' && confirm('你想清理所有cookie吗?')) {
var arrCookie = document.cookie.split(';');
var arrLength = arrCookie.length;
var expireDate = new Date();
expireDate.setDate(expireDate.getDate()-1);
for(var i=0; i<arrLength; i++) {
var str = arrCookie[i].split('=')[0];
document.cookie = str+ '=' + ';expires=' + expireDate.toGMTString();
}
}
}

</script>
</head>

<body οnlοad="startTime()">
<div id="div1"></div> <br />

<p>
<input type="button" value="开始计时" οnclick="startTime()"/>
<input type="text" id="txt">
<input type="button" value="停止计时" οnclick="endTime()"/>
</p>
<button οnclick="windowMethod()">JS Window对象</button>
<button οnclick="window.open();">打开新窗口</button>
<button οnclick="window.close();">关闭窗口</button>
<button οnclick="screenMethod()">JS Screen</button>
<button οnclick="locationMethod()">JS Location</button>
<button οnclick="historyMethod()">JS History</button>
<button οnclick="navigatorMethod()">JS Navigator</button>
<button οnclick="popupAlert()">JS popupAlert</button>
<button οnclick="timing1()">JS timing</button>
<button οnclick="cookieMethod()">JS Cookies</button>
<button οnclick="deleCookieMethod()">JS Delete Cookies</button>
</body>
</html>


@yinhuidasha.longyilu.tianhequ.guangzhoushi.guangdongsheng

这篇关于1.0.6 js window对象(window,location,screen,history,popupAlert,timing,cookie)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/892112

相关文章

SpringBoot项目中Redis存储Session对象序列化处理

《SpringBoot项目中Redis存储Session对象序列化处理》在SpringBoot项目中使用Redis存储Session时,对象的序列化和反序列化是关键步骤,下面我们就来讲讲如何在Spri... 目录一、为什么需要序列化处理二、Spring Boot 集成 Redis 存储 Session2.1

Java实例化对象的​7种方式详解

《Java实例化对象的​7种方式详解》在Java中,实例化对象的方式有多种,具体取决于场景需求和设计模式,本文整理了7种常用的方法,文中的示例代码讲解详细,有需要的可以了解下... 目录1. ​new 关键字(直接构造)​2. ​反射(Reflection)​​3. ​克隆(Clone)​​4. ​反序列化

C++类和对象之初始化列表的使用方式

《C++类和对象之初始化列表的使用方式》:本文主要介绍C++类和对象之初始化列表的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C++初始化列表详解:性能优化与正确实践什么是初始化列表?初始化列表的三大核心作用1. 性能优化:避免不必要的赋值操作2. 强

VSCode中配置node.js的实现示例

《VSCode中配置node.js的实现示例》本文主要介绍了VSCode中配置node.js的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一.node.js下载安装教程二.配置npm三.配置环境变量四.VSCode配置五.心得一.no

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例