`

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 onload="startTime()">
<div id="div1"></div> <br />

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






                @yinhuidasha.longyilu.tianhequ.guangzhoushi.guangdongsheng
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics