一、输出内容
<script type="text/javascript">
var mystr="我在学";
var mychar="JavaScript";
document.write(mystr+mychar+"我爱js"+"<br>");
</script>
结果:
二、警告(alert弹出框)
<title>alert</title>
<script type="text/javascript">
function rec(){
var mychar="I love JavaScript";
alert(mychar);
alert(“我是大漂亮”);
}
</script>
</head>
<body>
<input name="button" type="button" onClick="rec()" value="点击我,弹出对话框" />
</body>
结果:
三、JavaScript-确认(confirm 消息对话框)
<script type="text/javascript">
function rec(){
var mymessage=confirm("我是宝宝") ;
if(mymessage==true)
{
document.write("你是女士!");
}
else
{
document.write("你是男士!");
}
}
</script>
</head>
<body>
<input name="button" type="button" onClick="rec()" value="点击我,弹出确认对话框" />
</body>
四、JavaScript-提问(prompt 消息对话框)
<script type="text/javascript">
function rec(){
var myname=prompt("请输入你的姓名:");
if(myname!=null)
{ alert("你好"+myname); }
else
{ alert("你好 my friend."); }
}
</script>
</head>
<body>
<input name="button" type="button" onClick="rec()" value="点击我,对成绩做评价!" />
</body>
五、JavaScript-打开新窗口(window.open)
<script type="text/javascript">
function Wopen(){
window.open('http://www.imooc.com','_blank',width=600,height=400,top=100,left=0)
}
</script>
</head>
<body>
<input name="button" type="button" onClick="Wopen()" value="点击我,打开新窗口!" / >
</body>
六、JavaScript-关闭窗口(window.close)
<script type="text/javascript">
function wclose(){
var mywin=window.open("http://www.imooc.com");
mywin.close();
}
</script>
</head>
<body>
<input name="button" type="button" onClick="wclose()" value="点击我"/>
</body>
七、综合案例
<script type="text/javascript">
function openWindow(){
var open=confirm("确认打开新窗口?")
if(open==true)
{var url=prompt("通过输入对话框,确定打开的网址,默认为","http://www.imooc.com/")
if(url!=null)
{window.open(url,"_blank",'width=400,height=500,memubar=no,toolbar=no');}
else
{alert("再见");}
}
else
{alert("再见");}
}
</script>
</head>
<body>
<input type="button" value="新窗口打开网站" οnclick="openWindow()" />
</body>