JavaScript (三)

<script>
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
myFather=new person("John","Doe",50,"blue");
document.write(myFather.firstname + " is " + myFather.age + " years old.");
</script>
JavaScript 不定义不同类型的数字,比如整数、短、长、浮点等等。
在JavaScript中,数字不分为整数类型和浮点型类型,所有的数字都是由 浮点型类型。JavaScript采用IEEE754标准定义的64位浮点格式表示数字,它能表示最大值为±1.7976931348623157 x 10308,最小值为±5 x 10 -324
字符串使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置
字符串大小写转换使用函数 toUpperCase() / toLowerCase():
 Math 对象的 round 方法对一个数进行四舍五入。 Math 对象的 floor() 方法和 random() 来返回一个介于 0 和 10 之间的随机数

/i/区分大小写
/gi/不区分大小写
The test()方法搜索字符串指定的值,根据结果并返回真或假。
exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。

浏览器BOM:浏览器对象模型(Browser Object Model (BOM))
窗口高度和宽度:(不同浏览器不同版本不一样的写法)
var w=window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;

var h=window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
window.screen 对象包含有关用户屏幕的信息。使用时可以不用window这个前缀
screen.availWidth - 可用的屏幕宽度 1366
screen.availHeight - 可用的屏幕高度 728
window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面。
location.hostname 返回 web 主机的域名
location.pathname 返回当前页面的路径和文件名
location.port 返回 web 主机的端口 (80 或 443)
location.protocol 返回所使用的 web 协议(http:// 或 https://)
location.href

history.back() 方法加载历史列表中的前一个 URL。
history.forward() - 与在浏览器中点击按钮向前相同
window.navigator 对象包含有关访问者浏览器的信息。
<script>
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>";
document.getElementById("example").innerHTML=txt;
</script>
可以在 JavaScript 中创建三种消息框:警告框、确认框、提示框。
JavaScript 一个设定的时间间隔之后来执行代码,我们称之为计时事件
setInterval() - 间隔指定的毫秒数不停地执行指定的代码。
setTimeout() - 暂停指定的毫秒数后执行指定的代码
Note: setInterval() 和 setTimeout() 是 HTML DOM Window对象的两个方法。
setInterval(function(){alert("Hello")},3000);
clearInterval() 方法用于停止 setInterval() 方法执行的函数代码。
语法window.clearInterval(intervalVariable)
clearTimeout() 方法用于停止执行setTimeout()方法的函数代码。

Cookies 用于存储 web 页面的用户信息。
Cookies 的作用就是用于解决 "如何记录客户端的用户信息":

当用户访问 web 页面时,他的名字可以记录在 cookie 中。
在用户下一次访问该页面时,可以在 cookie 中读取用户访问记录。
JavaScript 可以使用 document.cookie 属性来创建 、读取、及删除 cookies。
<!DOCTYPE html>
<html>
<head>
<script>
function setCookie(cname,cvalue,exdays)
{
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname+"="+cvalue+"; "+expires;
}
function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) 
  {
  var c = ca[i].trim();
  if (c.indexOf(name)==0) return c.substring(name.length,c.length);
  }
return "";
}
function checkCookie()
{
var user=getCookie("username");
if (user!="")
  {
  alert("Welcome again " + user);
  }
else 
  {
  user = prompt("Please enter your name:","");
  if (user!="" && user!=null)
    {
    setCookie("username",user,30);
    }
  }
}
</script>
</head>
<body οnlοad="checkCookie()">
</body>
</html>






















猜你喜欢

转载自blog.csdn.net/Decadent_2014/article/details/46926305