Пример 1: знакомство с командой alert и prompt
<html>
<head>
<title>A Simple Page</title>
<script language="javascript">
<!--
yourname = prompt("What is your name?","Type your name here ...");
alert(yourname);
yourname2 = yourname;
alert(yourname2);
yourname = prompt("Type something different in","Something different ...");
alert(yourname);
alert(yourname2);
// -->
</script>
</head>
<body>
</body>
</html>
Аналогичный пример без декларации:
<html>
<head>
<title>A Simple Page</title>
<script language="javascript">
<!--
yourname = prompt("What is your name?","Type your name here ...");
alert(yourname);
yourname2 = yourname;
alert(yourname2);
yourname = prompt("Type something different in","Something different ...");
alert(yourname);
alert(yourname2);
// -->
</script>
</head>
<body>
</body>
</html>
Пример 2: Подсчёт арифметических выражений:
<HTML>
<BODY>
<SCRIPT language="JavaScript">
<!--
var myVar=2000;
document.write(myVar+"<BR>");
myVar+=2000;
document.write(myVar+"<BR>");
myVar=myVar-500;
document.write(myVar+"<BR>");
myVar=myVar*0;
document.write(myVar+"<BR>");
myVar=myVar+500;
document.write(myVar+"<BR>");
myVar-=80;
document.write(myVar+"<BR>");
//-->
</SCRIPT>
</BODY>
</HTML>
Пример 3: оператор typeof (определение типа)
<html>
<head>
<title>Using typeof to determine the type of variables</title>
<script language="JavaScript1.1">
<!--
var bMyVar = true;
var nMyVar = 35;
var sMyVar = "This is a string";
var uMyVar;
-->
</script>
</head>
<body>
<script language="JavaScript1.1">
<!--
document.writeln("bMyVar = " + typeof(bMyVar));
document.writeln("<br>nMyVar = " + typeof(nMyVar));
document.writeln("<br>sMyVar = " + typeof(sMyVar));
document.writeln("<br>uMyVar = " + typeof(uMyVar));
-->
</script>
</body>
</html>
Пример 4: диалог с пользователем 1
<html>
<head>
<script type='text/javascript'></script>
</head>
<body>
<FORM name="f1">
<INPUT TYPE=button
VALUE="call confirm"
onClick="if(window.confirm('look your answer on bottom?')==true)
{
document.f1.elements[1].value='YOUR ANSWER';}
else
{ document.f1.elements[1].value='empty)';
};">
<br>
<input type="text" name="el1">
<BR>
</form>
</body>
</html>
Пример 5: диалог с пользователем 2
<html>
<head>
<script type='text/javascript'></script>
</head>
<body>
<FORM name="f2">
<INPUT TYPE=button
VALUE="Open window"
onClick="document.f2.elements[1].
value=window.prompt('Enter text');">
<INPUT SIZE=30>
</form>
</body>
</html>
Пример 6: диалог с пользователем 3
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
body {
background-color:#ff0;
}
</style>
<script type="text/javascript">
window.onload=function() {
display();
}
function display(){
var name=prompt("What is your full name?",'Your Name Here');
var company=prompt("What is your company?",'Company Name');
var number=prompt("What is your ID number?",'123456');
document.getElementById('nme').firstChild.nodeValue='Name: '+name;
document.getElementById('cmpny').firstChild.nodeValue='Company: '+company;
document.getElementById('idno').firstChild.nodeValue='Employee Number: '+number;
document.getElementById('info').firstChild.nodeValue='Employee Data Complete!';
}
</script>
</head>
<body>
<h1>Data Entered:</h1>
<hr>
<div id="nme"> </div>
<div id="cmpny"> </div>
<div id="idno"> </div>
<hr>
<h2 id="info"> </h2>
</body>
</html>
Пример 7: построение таблицы умножения
<Script>
var i, j;
document.write("<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" align=\"center\">")
for (i = 2; i <= 10; i++)
{document.write("<tr>");
for (j = 2; j < 10; j++)
{document.write("<td>" + j + "×" + i + "=" + (i * j) + "</td>")}
document.write("</tr>")
}
document.write("</table>")
</Script>