1、Math.random()函数
<html>
<head>
<title>Javascript简单示例</title>
<script language="javascript">
<!--
scope= prompt("请输入一个正整数:");
for(i= 1; i <= 100; i++){
num= Math.floor(Math.random() * scope);//0-(scope-1)之间的随机数
document.write(num,"");
}
//-->
</script>
</head>
<body>
</body>
</html>
2、单击按钮后调用函数
<html>
<head>
<title>Javascript简单示例</title>
<script language="javascript">
<!--
functionfunc1(){
alert("按钮单击后调用的函数1!");
}
functionfunc2(){
alert("按钮单击后调用的函数2!");
}
//-->
</script>
</head>
<body>
<!--单击后调用两个函数用”,“隔开 -->
<inputtype="button" value="单击我"onClick="func1(), func2()" />
</body>
</html>
3、使用对象:同样使用function。
<html>
<head>
<title>Javascript简单示例</title>
</head>
<body>
<script language="javascript">
<!--
functionStudent(name, school, grade){
this.name= name;//注意这里要用this
this.school=school;
this.grade= grade;
}
hui= new Student("noting_gonna", "XX学校", "小学二年级");
//使用with可以省略对象名
with(hui){
document.write(name+ ": " + school + "," + grade + "<br />");
}
if(window.hui){
document.write("hui这个对象存在");
}
else
document.write("hui这个对象不存在");
//-->
</script>
</body>
</html>
4、获取input(text)中的内容 :name.value
<html>
<head>
<title>Javascript简单示例</title>
</head>
<body>
<script language="javascript">
<!--
functiongetLoginMsg(){
//以下也达到了省略对象名称的作用
loginMsg= document.loginForm;
alert("账号:" +loginMsg.userID.value + "\n" + "密码:" +loginMsg.password.value);
}
functionsetLoginMsg(Object){
alert(Object.id);
}
//-->
</script>
<formname="loginForm">
账号:<inputtype="text" name="userID" /><br />
密码:<inputtype="text" name="password" /><br />
<inputtype="button" value="登录"οnclick="getLoginMsg()" />
记住密码<inputtype="checkbox" id="这是checkbox的id"οnclick="setLoginMsg(this)" />
</form>
</body>
</html>
5、Javascript获取CSS的类名
<html>
<head>
<title>Javascript简单示例</title>
<style type="text/css">
<!--
.bgcolor{
background-color:#99330;
}
-->
</style>
</head>
<body>
<inputtype="button" value="点击"id="botton" οnclick=alert("使用的CSS类名:"+botton.className)class="bgcolor" />
</body>
</html>
6、setTimeout():在指定的时间调用函数
<html>
<head>
<title>Javascript简单示例</title>
</head>
<body>
<script language="javascript">
<!--
functionalarm(){
alert(new Date().toLocaleTimeString());
}
now= new Date().toLocaleTimeString();
document.write("当前的时间:"+ now + "<br />" + "2秒后弹出对话框!");
setTimeout(“alarm()”,2000);
//-->
</script>
</body>
</html>
7、setInterval():在指定时间内反复调用函数
<html>
<head>
<title>Javascript简单示例</title>
</head>
<body>
<script language="javascript">
<!--
functionclock(){
now= new Date().toLocaleTimeString();
time.value= now;
}
setInterval(“clock()”,1000);
//-->
</script>
当前的时间:<inputtype="text" id="time" size = 10/>
</body>
</html>
8、显示在页面的停留时间
<html>
<head>
<title>Javascript简单示例</title>
</head>
<body>
<script language="javascript">
<!--
old= new Date();
functionfunc(){
now= new Date();
diff= (now.getTime() - old.getTime())/1000 //获取相隔的毫秒数
diff= Math.round(diff);//返回x最接近的整数,对小数部分进行四舍五入
second= diff % 60;
minute= Math.round((diff-30)/60);//注意这里显示减去30,再除60,最后四舍五入
second = (second < 10) ?"0" + second : second;
minute= (minute < 10) ? "0" + minute : minute;
time.value= minute + ":" + second;
}
setInterval("func()",1000);
//-->
</script>
你在本页面停留的时间:<inputtype="text" id="time" size = 10/>
</body>
</html>
9、navigator.userAgent获取浏览器的名称及其版本、和所使用的操作等
<html>
<head>
<title>Javascript简单示例</title>
</head>
<body>
<script type="text/javascript">
<!--
varSys = {};
varua = navigator.userAgent.toLowerCase();
document.write(ua+"<br/>");
if(window.ActiveXObject)
Sys.ie= ua.match(/msie ([\d.]+)/)[1]
elseif (document.getBoxObjectFor)
Sys.firefox= ua.match(/firefox\/([\d.]+)/)[1]
elseif (window.MessageEvent && !document.getBoxObjectFor)
Sys.chrome= ua.match(/chrome\/([\d.]+)/)[1]
elseif (window.opera)
Sys.opera= ua.match(/opera.([\d.]+)/)[1]
elseif (window.openDatabase)
Sys.safari= ua.match(/version\/([\d.]+)/)[1];
if(Sys.ie)document.write('IE: '+Sys.ie);
if(Sys.firefox)document.write('Firefox: '+Sys.firefox);
if(Sys.chrome)document.write('Chrome: '+Sys.chrome);
if(Sys.opera)document.write('Opera: '+Sys.opera);
if(Sys.safari)document.write('Safari: '+Sys.safari);
//>
</script>
</body>
</html>
10、查看浏览器上所安装的插件:
<html>
<head>
<title>Javascript简单示例</title>
</head>
<body>
<script type="text/javascript">
<!--
num= navigator.plugins.length;
document.write("你浏览器上安装的插件有:<br/>");
for(i= 0; i <= num; i++){
name= navigator.plugins[i].name;
filename= navigator.plugins[i].filename;
document.write(name+"-------------------------------->" + filename + "<br/>");
}
//>
</script>
</body>
</html>