<script>
person={name:"yq",sex:"man",age:"22"};
document.write(person.name + " is " + person.age + " years old.");
</script>
<script>
function person(name,age){
this.name=name;
this.age=age;
}
myFather=new person("John",50,"blue");
document.write(myFather.name + " is " + myFather.age + " years old.");
</script>
<script>
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
//方法作为一个属性 或者直接调用
this.changeName=changeName;
//定义函数
function changeName(name){
this.lastname=name;
}
}
myMother=new person("Sally","Rally",48,"green");
myMother.changeName("Doe");
document.write(myMother.lastname);
</script>
<script>
var x;
document.write("<p>Only 17 digits: ");
x=12345678901234567890;
document.write(x + "</p>");
document.write("<p>0.2 + 0.1 = ");
x=0.2+0.1;
document.write(x + "</p>");
document.write("<p>It helps multiplying and dividing by 10: ");
x=(0.2*10+0.1*10)/10;
document.write(x +"</p>");
</script>
<script>
//八进制 16进制
var y = 0377;
var z = 0xFF;
document.write(y + "<br>");
document.write(z + "<br>");
</script>
<script>
var myNumber = 128;
document.write(myNumber + ' decimal<br>');
document.write(myNumber.toString(16) + ' hex<br>');
document.write(myNumber.toString(8) + ' octal<br>');
document.write(myNumber.toString(2) + ' binary<br>');
</script>
<script>
myNumber=2;
while (myNumber!=Infinity)
{
myNumber=myNumber*myNumber;
document.write(myNumber +'<BR>');
}
</script>
<script>
//正无穷 负无穷
var x = 2/0;
var y = -2/0;
document.write(x + "<br>");
document.write(y + "<br>");
</script>
<script>
function myFunction()
{
var str=document.getElementById("p1").innerHTML;
//获取字符串的位置 位置加上1
var n=str.indexOf("the");
document.getElementById("p2").innerHTML=n+1;
}
</script>
<script>
var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("world!"));
</script>
<script>
function myFunction()
{
var str="a,b,c,d,e,f";
//分割 变成数组
var n=str.split(",");
document.getElementById("demo").innerHTML=n[1];
}
</script>
<script>
function myFunction()
{
var d = new Date();
var x = document.getElementById("demo");
x.innerHTML=d.getFullYear();
}
</script>
<script>
function myFunction()
{
var d = new Date();
var x = document.getElementById("demo");
x.innerHTML=d.toUTCString();
}
</script>
<script>
function myFunction()
{
var d = new Date();
//月份从0开始
d.setFullYear(2020,3,3);
var x = document.getElementById("demo");
x.innerHTML=d;
}
</script>
<script>
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
h=checkTime(h);
m=checkTime(m);
s=checkTime(s);
document.getElementById("txt").innerHTML=h+":"+m+":"+s+"</br>";
//小写
t= setTimeout(function(){ startTime() },500);
}
//设置间隔
//setInterval("startTime()",1000);
function checkTime(i)
{
if(i<10){
i="0"+i;
}
return i;
}
</script>
</head>
<body onload="startTime()">
<div id="txt"></div>
<script>
Array.prototype.myCase=function(){
for(var i=0;i<this.length;i++){
// this[i]=this[i].toUpperCase();
this[i]=this[i].toLowerCase();
}
}
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.myCase();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
<script>
//合并数组
function myFunction()
{
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);
var x=document.getElementById("demo");
x.innerHTML=children;
}
</script>
<script>
var parents = ["Jani", "Tove"];
var brothers = ["Stale", "Kai Jim", "Borge"];
var children = ["Cecilie", "Lone"];
var family = parents.concat(brothers, children);
document.write(family);
</script>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x=document.getElementById("demo");
x.innerHTML=fruits.join();
}
</script>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function myFunction()
{
//排序,字母排序
fruits.pop();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function myFunction()
{
fruits.push("yqing");
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function myFunction()
{
fruits.reverse();
//反转
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
<script>
var fruits = ["banana", "Orange", "Apple", "Mango"];
function myFunction()
{
//从头开始删除一个
fruits.shift();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
//包前不包后
var citrus = fruits.slice(1,3);
var x=document.getElementById("demo");
x.innerHTML=citrus;
}
</script>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//排序
fruits.sort();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
<script>
function myFunction()
{
var points = [40,100,1,5,25,10];
//排序函数 a-b从小到大 b-a从大到小
points.sort(function(a,b){return a-b;});
var x=document.getElementById("demo");
x.innerHTML=points;
}
</script>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//替换数组中的元素
fruits.splice(2,0,"y","q");
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//从头插入两个
fruits.unshift("y","q");
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
<script>
var b1=new Boolean(0);
var b2=new Boolean(1);
var b3=new Boolean("");
var b4=new Boolean(null);
var b5=new Boolean(NaN);
var b6=new Boolean("false");
document.write("0 is boolean "+ b1 +"<br>");
document.write("1 is boolean "+ b2 +"<br>");
document.write("An empty string is boolean "+ b3 + "<br>");
document.write("null is boolean "+ b4+ "<br>");
document.write("NaN is boolean "+ b5 +"<br>");
//字符串转为布尔值
document.write("The string 'false' is boolean "+ b6 +"<br>");
</script>
注意:当使用构造函数创造正则对象时,需要常规的字符转义规则(在前面加反斜杠 \)。比如,以下是等价的:
var re = new RegExp("\\w+");
var re = /\w+/;
<script>
var str = "Visit W3CSchool";
var patt1 = /ool/i;
//捕获到内容
document.write(str.match(patt1));
</script>
<script>
var str="Is this all there is?";
var patt1=/is/g;
//全局捕获
document.write(str.match(patt1));
</script>
<script>
//var patt1=new RegExp("e");
var patt1=/e/;
document.write(patt1.test("The best things in life are free"));
</script>
<script>
var str="Is this all there is?";
var patt1=/is/ig;
document.write(str.match(patt1));
</script>
<script>
var str = 'runoob';
var patt1 = new RegExp('\\w','g');; // 有转义作为正则表达式处理
var patt2 = new RegExp('\w', 'g'); // 无转义作为字符串处理
var patt3 =/\w+/g; // 与 patt1 效果相同
document.write(patt1.test(str)) //输出 true
document.write("<br>")
document.write(patt2.test(str)) //输出 false
document.write("<br>")
document.write(patt3.test(str)) //输出 true
</script>
<script>
var w=window.innerWidth
document.documentElement.clientWidth
document.body.clientWidth
var h=window.innerHeight
document.documentElement.clientHeight
document.body.clientHeight;
x=document.getElementById("demo");
x.innerHTML="Browser inner window width: " + w + ", height: " + h + "."
</script>
<script>
function newDoc()
{
//location.href assign()
location.assign("http://www.baidu.com");
}
</script>
</head>
<body>
<input type="button" value="加载新文档" onclick="newDoc()">
</body>
<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>
<script>
function myFunction()
{
var x;
var res=confirm("确定吗?");
if(res)
{
x="你按下了\"确定\"按钮!";
}
else
{
x="你按下了\"取消\"按钮!";
}
document.getElementById("demo").innerHTML=x;
}
</script>
<script>
function myFunction()
{
var x;
var person=prompt("请输入名字?","yqing");
if(person!=null && person!="")
{
x="你好 " + person + "! 今天感觉如何?";
document.getElementById("demo").innerHTML=x;
}
}
</script>
// 直接写入函数 或者 双引号调用函数名
setInterval(function(){alert("Hello")},3000);
<script>
//写入函数
//var myVar=setInterval(funciton(){myTimer()},1000);
//函数名调用
var myVar=setInterval("myTimer()",1000);
function myTimer()
{
var d=new Date();
//转为本地时间字符串
var t=d.toLocaleTimeString();
document.getElementById("demo").innerHTML=t;
}
</script>
<script>
var myVar=setInterval(function(){myTimer()},1000);
function myTimer()
{
var d=new Date();
var t=d.toLocaleTimeString();
document.getElementById("demo").innerHTML=t;
}
function myStopFunction()
{
//清楚定时器
clearInterval(myVar);
}
</script>
<button onclick="myStopFunction()">Stop time</button>
<script>
var myVar=setInterval(function(){myTimer()},1000);
function myTimer()
{
var d=new Date();
var t=d.toLocaleTimeString();
document.getElementById("demo").innerHTML=t;
}
function myStopFunction()
{
//清楚定时器
clearInterval(myVar);
}
</script>
您可以使用 path 参数告诉浏览器 cookie 的路径。默认情况下,cookie 属于当前页面。
document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 GMT; path=/"
删除 cookie 非常简单。您只需要设置 expires 参数为以前的时间即可,如下所示,设置为 Thu, 01 Jan 1970 00:00:00 GMT:
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
打开App,阅读手记