一.window尺寸:
有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)。
对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:
window.innerHeight - 浏览器窗口的内部高度
window.innerWidth - 浏览器窗口的内部宽度
对于 Internet Explorer 8、7、6、5:
document.documentElement.clientHeight
document.documentElement.clientWidth
或者
document.body.clientHeight
document.body.clientWidth
获取方案:
<script>
var w=window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h=window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
document.write(w+" "+h);
</script>
二.Window Screen
<script>
document.write("屏幕可用宽度:"+screen.availWidth+"屏幕可用高度:"+screen.availHeight);
</script>
三.Window Location
location.hostname 返回 web 主机的域名
location.pathname 返回当前页面的路径和文件名
location.port 返回 web 主机的端口 (80 或 443)
location.protocol 返回所使用的 web 协议(http:// 或 https://)
加载新的文档:
<script>
function ff()
{
location.assgin("http://www.youkuaiyun.com");
}
</script>
<button type="button" οnclick="ff()">点击
</button>
四.JavaScript Window History
history.back();//与浏览器的返回键相同
history.forward();//与浏览器前进键相同
五.消息框
1.警告框
警告框经常用于确保用户可以得到某些信息。
当警告框出现后,用户需要点击确定按钮才能继续进行操作。
alert("文本内容");
2.确认框
确认框用于使用户可以验证或者接受某些信息。
当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作。
如果用户点击确认,那么返回值为 true。如果用户点击取消,那么返回值为 false。
confirm("文本内容");
3.提示框
提示框经常用于提示用户在进入页面前输入某个值。
当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。
如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。
prompt("文本内容","默认值");
六.JavaScript 计时
通过使用 JavaScript,我们有能力做到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。我们称之为计时事件。
setTimeout();//要区别于setInterval setTimeout延迟时间执行且仅执行一次
clearTimeout();//清除定时器
报时器:
<html>
<head>
<script type="text/javascript">
function startTime()//利用递归来实现setInterval
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
s=checkTime(s)
document.getElementById('txt').innerHTML=h+":"+m+":"+s
t=setTimeout('startTime()',500);//返回setTimeout对象,可用清除计时器
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
function stopTime()
{
clearTimeout(t);//清除计时器
}
</script>
</head>
<body>
<div id="txt"></div>
<input type="button" οnclick="startTime()" value="点击开始报时">
<input type="button" οnclick="stopTime()" value="点击停止报时">
</body>
</html>
七.JavaScript Cookies
</pre><pre name="code" class="javascript"><html>
<head>
<script type="text/javascript">
function getCookie(c_name)//获取cookie内容
{
if (document.cookie.length>0)//返回null则为没有cookie
{
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));//<span style="font-family: Arial, Helvetica, sans-serif;">unescape对字符串进行</span>解码
}
}
return ""
}
function setCookie(c_name,value,expiredays)//创建cookie
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());//<span style="font-family: Arial, Helvetica, sans-serif;">escape对字符串进行编码</span>
}
function checkCookie()//检查cookie
{
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);
}
}
}
</script>
</head>
<body onLoad="checkCookie()">
</body>
</html>
cookie判断 创建 获取