js笔记

来源:互联网 发布:redis mysql的区别 编辑:程序博客网 时间:2024/06/09 20:49

//外部脚本不能包含 <script> 标签。





//赋值,输出,继续赋值输出(var document)
<script>
var firstname;
firstname="Geprage";
document.write(firstname);
document.write("<br/>");
firstname="John";
document.write(firstname);
</script>




//获取当前时间(new time/ d.getHours)
<script>
var d=new Date()  //当前时间
var time=d.getHours()    
if (time<10)
{
document.write("<b>早安</b>")
}
else{
document.write("<b>祝您愉快</b>")
}
</script>






//系统随机(Math.random)
<script type="text/javascript">
var r=Math.random()     //系统随机
if (r>0.5) 
{
document.write("<a href='http://www.w3school.com.cn'>学习 Web 开发!</a>")
}
else
{
document.write("<a href='http://www.microsoft.com'>访问微软!</a>")
}
</script>






//警告框(alert)
<script type="text/javascript">
function disp_alert()
{
alert("再次向您问好!在这里,我们向您演示" + '\n' + "如何向警告框添加折行。")
}
</script>
</head>
<body>


<input type="button" onclick="disp_alert()" value="显示警告框" />


</body>






//确认框(confirm)
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button!");//r=确认
if (r==true)
  {
  alert("You pressed OK!");
  }
else
  {
  alert("You pressed Cancel!");
  }
}
</script>
</head>
<body>


<input type="button" onclick="show_confirm()" value="Show a confirm box" />


</body>




//提示框(prompt)
<head>
<script type="text/javascript">
function disp_prompt()//处理-提示
  {
  var name=prompt("请输入您的名字","Bill Gates")
  if (name!=null && name!="")
    {
    document.write("你好!" + name + " 今天过得怎么样?")
    }
  }
</script>
</head>
<body>


<input type="button" onclick="disp_prompt()" value="显示提示框" />


</body>








//带参数的函数调用
<head>


<script type="text/javascript">
function myfunction(txt)
{
alert(txt)
}
</script>


</head>
<body>


<form>
<input type="button" onclick="myfunction('您好!')" value="调用函数">
</form>


<p>通过点击这个按钮,可以调用一个带参数的函数。该函数会输出这个参数。</p>


</body>




//带参数的返回值的函数
<head>
<script type="text/javascript">
function product(a,b)
{
return a*b
}
</script>
</head>


<body>
<script type="text/javascript">
document.write(product(6,5))
</script>


<p>body 部分中的脚本调用一个带有两个参数(6 和 5)的函数。</p>
<p>该函数会返回这两个参数的乘积。</p>


</body>






//JavaScript 循环


//for循环,先循环再累加(运算)
for (i = 1; i <= 6; i++)
{
document.write("<h" + i + ">这是标题 " + i)
document.write("</h" + i + ">")
}


//while循环
i = 0
while (i <= 5)
{
document.write("数字是 " + i)
document.write("<br />")
i++
}


//do while循环
i = 0
do
{
document.write("数字是 " + i)
document.write("<br />")
i++
}


//breake 语句
var i=0
for (i=0;i<=10;i++)
{
if (i==3){break}
document.write("数字是 " + i)
document.write("<br />")
}
/*结果:(到三结束)
数字是 0
数字是 1
数字是 2
*/


//continue 语句
var i=0
for (i=0;i<=10;i++)
{
if (i==3){continue}
document.write("数字是 " + i)
document.write("<br />")
/*结果:(没有三)
数字是 0
数字是 1
数字是 2
数字是 4
数字是 5
数字是 6
数字是 7
数字是 8
数字是 9
数字是 10*/






//For....in遍历数组
<script type="text/javascript">
var x
var mycars = new Array()
mycars[0] = "宝马"
mycars[1] = "奔驰"
mycars[2] = "宾利"


for (x in mycars)
{
document.write(mycars[x] + "<br />")
}
</script>








//高级 JavaScript 实例
//检测浏览器版本
<script type="text/javascript">
var browser=navigator.appName//浏览器名字
var b_version=navigator.appVersion//完整版浏览器
var version=parseFloat(b_version)//取浏览器版本前面的有效数字
document.write("浏览器名称:"+ browser)
document.write("<br />")
document.write("浏览器版本:"+ version)
</script>


//浏览器信息
<script type="text/javascript">
document.write("<p>浏览器:")
document.write(navigator.appName + "</p>")//浏览器:Netscape


document.write("<p>浏览器版本:")
document.write(navigator.appVersion + "</p>")<!--浏览器版本:5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36 OPR/36.0.2130.65-->


document.write("<p>代码:")
document.write(navigator.appCodeName + "</p>")//代码:Mozilla


document.write("<p>平台:")
document.write(navigator.platform + "</p>")//平台:Win32


document.write("<p>Cookies 启用:")
document.write(navigator.cookieEnabled + "</p>")//Cookies 启用:true


document.write("<p>浏览器的用户代理报头:")
document.write(navigator.userAgent + "</p>")<!--浏览器的用户代理报头:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36 OPR/36.0.2130.65-->
</script>


//浏览器全部信息
<script type="text/javascript">
var x = navigator;
document.write("CodeName=" + x.appCodeName);
document.write("<br />");
document.write("MinorVersion=" + x.appMinorVersion);
document.write("<br />");
document.write("Name=" + x.appName);
document.write("<br />");
document.write("Version=" + x.appVersion);
document.write("<br />");
document.write("CookieEnabled=" + x.cookieEnabled);
document.write("<br />");
document.write("CPUClass=" + x.cpuClass);
document.write("<br />");
document.write("OnLine=" + x.onLine);
document.write("<br />");
document.write("Platform=" + x.platform);
document.write("<br />");
document.write("UA=" + x.userAgent);
document.write("<br />");
document.write("BrowserLanguage=" + x.browserLanguage);
document.write("<br />");
document.write("SystemLanguage=" + x.systemLanguage);
document.write("<br />");
document.write("UserLanguage=" + x.userLanguage);
</script>




//提示浏览器更新
<head>
<script type="text/javascript">
function detectBrowser()
{
var browser=navigator.appName
var b_version=navigator.appVersion
var version=parseFloat(b_version)
if ((browser=="Netscape"||browser=="Microsoft Internet Explorer") && (version>=4))
  {alert("您的浏览器够先进了!")}
else
  {alert("是时候升级您的浏览器了!")}
}
</script>
</head>


<body onload="detectBrowser()">
</body>






//创建欢迎cookis
<html>
<head>
<script type="text/javascript">
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 ""
}


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())
}




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)         //定义cookies
    }
  }
}
</script>
</head>
<body onLoad="checkCookie()">
</body>
</html>




//按钮动画
<head>
<script type="text/javascript">
function mouseOver()
{
document.b1.src ="/i/eg_mouse.jpg"
}
function mouseOut()
{
document.b1.src ="/i/eg_mouse2.jpg"
}
</script>
</head>


<body>
<a href="/index.html" target="_blank">
<img border="0" alt="Visit W3School!" src="/i/eg_mouse2.jpg" name="b1"  onmouseover="mouseOver()" onmouseout="mouseOut()" /></a>
</body>



0 0
原创粉丝点击