javaScript 关于Windows



1 Windows 对象

<1>全部浏览器都支持 window 对象。它表示浏览器窗体。

<2>全部 JavaScript 全局对象、函数以及变量均自己主动成为 window 对象的成员。

<3>全局变量是 window 对象的属性。

<4>全局函数是 window 对象的方法。

window.document.getElementById("header");

2 Windows经常使用函数

<1>获取Windows尺寸

var w=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var h=window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

<2>其他常见函数

window.open() - 打开新窗体

window.close() - 关闭当前窗体

window.moveTo() - 移动当前窗体

window.resizeTo() - 调整当前窗体的尺寸





3 Screen对象

screen.availWidth - 可用的屏幕宽度

screen.availHeight - 可用的屏幕高度









4 Location对象

document.write(location.pathname +'<br/>'); 返回 web 主机的域名

document.write(location.hostname  +'<br/>');
返回当前页面的路径和文件名称

document.write(location.protocol  +'<br/>'); 返回所使用的 web 协议(http:// 或 https://

document.write(location.href); 返回正URL

location.assign("http://www.w3school.com.cn")
载入新的网页









5 History对象

window.history 对象包括浏览器的历史

history.back() - 与在浏览器点击后退button同样

history.forward() - 与在浏览器中点击button向前同样

示比例如以下:

function goForward()
{
window.history.forward()
}
</script>
</head>
<body> <input type="button" value="Forward" onclick="goForward()">

6 Navigator对象

window.navigator 对象包括有关訪问者浏览器的信息





navigator.appCodeName Mozilla

navigator.appName Netscape

navigator.appVersion 5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76Safari/537.36

navigator.cookieEnabled true

navigator.platform Win32

navigator.userAgent Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36

navigator.systemLanguage undefined





注意:来自 navigator 对象的信息具有误导性。不应该被用于检測浏览器版本号。这是由于:

navigator 数据可被浏览器使用者更改

浏览器无法报告晚于浏览器公布的新操作系统





7 javaScript消息框

<1> 警告框

警告框经经常使用于确保用户能够得到某些信息。当警告框出现后。用户须要点击确定button才干继续进行操作

alert("文本")

<2> 确认框 

确认框用于使用户能够验证或者接受某些信息。当确认框出现后,用户须要点击确定或者取消button才干继续进行操作。假设用户点击确认,那么返回值为 true。假设用户点击取消,那么返回值为 false

confirm("文本")

<3> 提示框

提示框经经常使用于提示用户在进入页面前输入某个值。

当提示框出现后。用户须要输入某个值,然后点击确认或取消button才干继续操纵。假设用户点击确认。那么返回值为输入的值。假设用户点击取消,那么返回值为 null。

var name=prompt("请输入您的名字",输入的默认值)





8 javaScript 计时器

有关计时的两个关键函数

setTimeOut() //设置一个计时器

clearTimeout() //取消一个计时器

setTimeout() 的第一个參数是含有 JavaScript 语句的字符串。这个语句可能诸如 "alert('5 seconds!')"。或者对函数的调用,诸如 alertMsg()"。

第二个參数指示从当前起多少毫秒后运行第一个參数。

示比例如以下:

 t=setTimeout("timedCount()",1000)

 clearTimeout(t)





9 javaScript Cookie

cookie 是存储于訪问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时。就会发送这个 cookie。

你能够使用 JavaScript 来创建和取回 cookie 的值

用cookie基本的过程:

设置cookie

获取cookie

检測cookie

<1> 设置cookie

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

<2> 获取cookie

function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}

<3> 检查cookie

function checkCookie()
{
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)
}
}
}

注意:

1>:字符串存进Cookie的时候,内容是累加,各段内容之间是通过分号";"隔开

2>:在使用键值对的形式存储cookie的时候,索引的keyword但是为中文,同一个keyword所具有的值会相互覆盖。

document.cookie ="动物=乌龟";
document.cookie ="动物=王八";

最后cookie中存的是document.cookie ="动物=王八";由于王八跟乌龟keyword同样。因此王八覆盖乌龟

3>:cookie 赋值

document.cookie = "xxx"; //or
document.cookie = "yyy=xxx"
05-12 13:14