1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>Title</title>
6 </head>
7 <body>
8 <input type="button" value="加载新文档" onclick="newDoc()">
9 <input type="button" value="上一页" onclick="goBack()">
10 <input type="button" value="下一页" onclick="goForward()"><br>
11 <hr><h1>在 JavaScript 中创建三种消息框:警告框、确认框、提示框</h1>
12 <input type="button" onclick="myFunction()" value="显示警告框"><br>
13 <p>点击按钮,显示确认框</p>
14 <button onclick="myFunction1()">点击显示确认框</button>
15 <p id="demo"></p>
16
17 <p>点击按钮查看输入的对话框</p>
18 <button onclick="myFunction2()">点击显示提示输入框</button>
19 <p id="demo1"></p>
20 <hr><h1>计时事件</h1>
21 <p>在页面显示一个时钟</p>
22 <button onclick="myStopFunction()">停止</button>
23 <p id="demo2"></p>
24 <button onclick="myFunction3()">等待3秒出现弹框</button><br>
25
26
27
28 <script>
29 document.write("1、(当前页面的)整个 URL:"+location.href+"<br>");//返回(当前页面的)整个 URL;
30 document.write("2、web 主机的域名:"+location.hostname+"<br>");// location.hostname 返回 web 主机的域名;;;
31 document.write("3、当前页面的路径和文件名:"+location.pathname+"<br>");//location.pathname 返回当前页面的路径和文件名
32 document.write("4、web 主机的端口 (80 或 443):"+location.port+"<br>");//location.port 返回 web 主机的端口 (80 或 443)
33 document.write("5、所使用的 web 协议(http: 或 https:):"+location.protocol+"<br>");//location.protocol 返回所使用的 web 协议(http: 或 https:)
34 document.write(location.host+"<br>");
35 document.write(location.origin+"<br>");
36
37 function newDoc() {
38 window.location.assign("https://www.baidu.com")
39 }
40
41 function goBack() {
42 window.history.back();
43 }
44 function goForward() {
45 window.history.forward();
46 }
47
48 function myFunction() {
49 alert("你好,\n我是一个警告框!");//弹窗使用 反斜杠 + "n"(\n) 来设置换行
50 }
51 function myFunction1() {
52 var x;
53 var r = confirm("显示需要确定的文字按下按钮");
54 if (r==true){
55 x="你按下了'确定'按钮!";
56 } else {
57 x="你按下了'取消'按钮!";
58 }
59 document.getElementById("demo").innerHTML=x;
60 }
61 function myFunction2() {
62 var x;
63 var person=prompt("请输入你的名字","");
64 if(person!=null && person!=""){
65 x="你好"+person+" 今天感觉如何?";
66 document.getElementById("demo1").innerHTML=x;
67 }
68 }
69
70 var myVar=setInterval(function () {myTimer()},1000);//1000 毫秒是一秒//setInterval() - 间隔指定的毫秒数不停地执行指定的代码
71 function myTimer() {
72 var d=new Date();
73 var t=d.toLocaleDateString()+d.toLocaleTimeString();
74 document.getElementById("demo2").innerHTML=t;
75 }
76 function myStopFunction() {
77 clearInterval(myVar);
78 }
79 function myFunction3() {//等待3秒后出现"Hello"弹框
80 myVar = setTimeout(function () {alert("hello")},3000);//setTimeout() - 在指定的毫秒数后执行指定代码
81 }
82
83 function setCookie(cname,cvalue,exdays){//创建一个函数用于存储访问者的名字
84 var d = new Date();
85 d.setTime(d.getTime()+(exdays*24*60*60*1000));
86 var expires = "expires="+d.toGMTString();
87 document.cookie = cname+"="+cvalue+"; "+expires;//document.cookie 将以字符串的方式返回所有的 cookie,类型格式: cookie1=value; cookie2=value; cookie3=value;
88 }
89 function getCookie(cname){//创建一个函数用于返回指定 cookie 的值
90 var name = cname + "=";
91 var ca = document.cookie.split(';');
92 for(var i=0; i<ca.length; i++) {
93 var c = ca[i].trim();
94 if (c.indexOf(name)==0) { return c.substring(name.length,c.length); }
95 }
96 return "";
97 }
98
99 </script>
100 </body>
101 </html>