◆ サンプル
<html>
<head>
<title>TEST</title>
</head>
<body>
<script type="text/javascript">
<!--
document.write("Hello world!!");
// -->
</script>
</body>
</html>
◆ 実行例
Hello world!!
◆ サンプル
<script type="text/javascript">
<!--
document.write("Browser: ", navigator.appName, "<br>");
document.write("Version: ", navigator.appVersion, "<br>");
document.write("Platform: ", navigator.platform, "<br>");
// -->
</script>
◆ 実行例
◆ サンプル
<script type="text/javascript">
<!--
if (navigator.appName == "Microsoft Internet Explorer") {
document.write("Internet Explorer です。");
} else {
document.write("Internet Explorer ではありません。");
}
// -->
</script>
◆ 実行例
◆ サンプル
<script type="text/javascript">
<!--
for (i = 0; i < 3; i++) {
document.write("Hello!!<br>");
}
// -->
</script>
◆ 実行結果
◆ サンプル
<script type="text/javascript">
<!--
function HELLO() {
alert("Hello!!");
}
// -->
</script>
<form action="">
<input type="button" value="TEST1" onclick="HELLO()">
<input type="button" value="TEST2" onclick="alert('Hello!!')">
</form>
◆ 実行例
◆ サンプル
<script type="text/javascript">
<!--
function KEISAN() {
var x = Number(document.f1.t1.value);
var y = Number(document.f1.t2.value);
document.f1.t3.value = x + y;
}
// -->
</script>
<form name="f1" action="">
<input type="text" name="t1" value="5">
+
<input type="text" name="t2" value="3">
<input type="button" value="=" onclick="KEISAN()">
<input type="text" name="t3">
</form>
◆ 実行例
◆ サンプル
<form action="">
<input type="button" value="Yahoo!"
onclick="location.href='http://www.yahoo.co.jp/'">
</form>
◆ 実行例
◆ サンプル
<form action="">
<input type="button" value="Yahoo!"
onclick="window.open('http://www.yahoo.co.jp/', '_blank')">
</form>
◆ 実行例
◆ サンプル
<script type="text/javascript">
<!--
function SHUUKEI() {
if (document.f2.c1.checked) { alert("わくわくですね。\n"); }
if (document.f2.c2.checked) { alert("どきどきですね。\n"); }
}
// -->
</script>
<form name="f2" action="">
<input type="checkbox" name="c1">わくわく
<input type="checkbox" name="c2">どきどき
<input type="button" value="集計" onclick="SHUUKEI()">
</form>
◆ 実行例
◆ サンプル
<script type="text/javascript">
<!--
function KEKKA() {
var i;
for (i = 0; i < document.f3.r1.length; i++) {
if (document.f3.r1[i].checked) {
alert(document.f3.r1[i].value + "ですね。");
}
}
}
// -->
</script>
<form name="f3" action="">
<input type="radio" name="r1" value="yes">YES
<input type="radio" name="r1" value="no">NO
<input type="button" value="結果" onclick="KEKKA()">
</form>
◆ 実行例