This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
<script type="text/javascript">document.write("<h1>This is a header</h1>");</script>
</body></html>
<html><head></head>
<body>
<script type="text/javascript">document.write("This message is written by JavaScript");</script>
</body></html>
<html><head><script type="text/javascript">function message(){alert("This alert box was called with the onload event");}</script></head>
<body onload="message()">
</body></html>
<html><head></head><body>
<script src="xxx.js"></script>
<p>The actual script is in an external script file called "xxx.js".</p>
</body></html>
<html><body>
<script type="text/javascript">document.write("<h1>This is a header</h1>");document.write("<p>This is a paragraph</p>");document.write("<p>This is another paragraph</p>");</script>
</body></html>
<html><body>
<script type="text/javascript">{document.write("<h1>This is a header</h1>");document.write("<p>This is a paragraph</p>");document.write("<p>This is another paragraph</p>");}</script>
</body></html>
<html><body>
<script type="text/javascript">// This will write a header:document.write("<h1>This is a header</h1>");// This will write two paragraphs:document.write("<p>This is a paragraph</p>");document.write("<p>This is another paragraph</p>");</script>
</body></html>
<html><body>
<script type="text/javascript">/*The code below will writeone header and two paragraphs*/document.write("<h1>This is a header</h1>");document.write("<p>This is a paragraph</p>");document.write("<p>This is another paragraph</p>");</script>
</body></html>
<html><body>
<script type="text/javascript">document.write("<h1>This is a header</h1>");document.write("<p>This is a paragraph</p>");//document.write("<p>This is another paragraph</p>");</script>
</body></html>
<html><body>
<script type="text/javascript">/*document.write("<h1>This is a header</h1>");document.write("<p>This is a paragraph</p>");document.write("<p>This is another paragraph</p>");*/</script>
<html><head><script type="text/javascript">function disp_alert(){alert("Hello again! This is how we" + '\n' + "add line breaks to an alert box!");}</script></head><body>
<input type="button" onclick="disp_confirm()" value="Display a confirm box" />
</body></html>
<html><head><script type="text/javascript">function disp_prompt(){var name=prompt("Please enter your name","Harry Potter");if (name!=null && name!="") { document.write("Hello " + name + "! How are you today?"); }}</script></head><body>
<input type="button" onclick="disp_prompt()" value="Display a prompt box" />
<p>The script in the body section calls a function with two parameters (4 and 3).</p><p>The function will return the product of these two parameters.</p></body></html>
<html><body>
<script type="text/javascript">for (i = 0; i <= 5; i++){document.write("The number is " + i);document.write("<br />");}</script>
<p>Explanation:</p>
<p>This for loop starts with i=0.</p>
<p>As long as <b>i</b> is less than, or equal to 5, the loop will continue to run.</p>
<p><b>i</b> will increase by 1 each time the loop runs.</p>
</body></html>
<html><body>
<script type="text/javascript">for (i = 1; i <= 6; i++)
{document.write("<h" + i + ">This is header " + i);document.write("</h" + i + ">");}</script>
</body></html>
<html><body>
<script type="text/javascript">i=0;while (i<=5){document.write("The number is " + i);document.write("<br />");i++;}</script>
<p>Explanation:</p>
<p><b>i</b> is equal to 0.</p>
<p>While <b>i</b> is less than , or equal to, 5, the loop will continue to run.</p>
<p><b>i</b> will increase by 1 each time the loop runs.</p>
</body></html>
<html><body>
<script type="text/javascript">i = 0;do{document.write("The number is " + i);document.write("<br />");i++;}while (i <= 5)</script>
<p>Explanation:</p>
<p><b>i</b> equal to 0.</p>
<p>The loop will run</p>
<p><b>i</b> will increase by 1 each time the loop runs.</p>
<p>While <b>i</b> is less than , or equal to, 5, the loop will continue to run.</p>
</body></html>
<html>
<body><script type="text/javascript">var i=0;for (i=0;i<=10;i++){if (i==3) { break; }document.write("The number is " + i);document.write("<br />");}</script><p>Explanation: The loop will break when i=3.</p></body></html>
<html><body><script type="text/javascript">var i=0;for (i=0;i<=10;i++){if (i==3) { continue; }document.write("The number is " + i);document.write("<br />");}</script>
<p>Explanation: The loop will break the current loop and continue with the next value when i=3.</p>
}catch(err) { txt="There was an error on this page.\n\n"; txt+="Error description: " + err.description + "\n\n"; txt+="Click OK to continue.\n\n"; alert(txt); }}</script></head>
<html><head><script type="text/javascript">var txt=""function message(){try { adddlert("Welcome guest!"); }catch(err) { txt="There was an error on this page.\n\n"; txt+="Click OK to continue viewing this page,\n"; txt+="or Cancel to return to the home page.\n\n"; if(!confirm(txt)) { document.location.href="http://www.w3schools.com/"; } }}</script></head>
function handleErr(msg,url,l){txt="There was an error on this page.\n\n";txt+="Error: " + msg + "\n";txt+="URL: " + url + "\n";txt+="Line: " + l + "\n\n";txt+="Click OK to continue.\n\n";alert(txt);return true;}
function message(){adddlert("Welcome guest!");}</script></head>
<html><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("Your browser is good enough!"); }else { alert("It's time to upgrade your browser!"); }}</script></head>
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); } }}</script></head><body onLoad="checkCookie()"></body></html>
<map id ="planetmap" name="planetmap"><area shape ="rect" coords ="0,0,82,126"onMouseOver="writeText('The Sun and the gas giant planets like Jupiter are by far the largest objects in our Solar System.')"href ="sun.htm" target ="_blank" alt="Sun" />
<area shape ="circle" coords ="90,58,3"onMouseOver="writeText('The planet Mercury is very difficult to study from the Earth because it is always so close to the Sun.')"href ="mercur.htm" target ="_blank" alt="Mercury" />
<area shape ="circle" coords ="124,58,8"onMouseOver="writeText('Until the 1960s, Venus was often considered a twin sister to the Earth because Venus is the nearest planet to us, and because the two planets seem to share many characteristics.')"href ="venus.htm" target ="_blank" alt="Venus" /></map>
<body><form><input type="button" value="Display timed alertbox!" onClick = "timedMsg()"></form><p>Click on the button above. An alert box will be displayed after 5 seconds.</p></body>
<body><form><input type="button" value="Display timed text!" onClick="timedText()"><input type="text" id="txt"></form><p>Click on the button above. The input field will tell you when two, four, and six seconds have passed.</p></body>
<body><form><input type="button" value="Start count!" onClick="timedCount()"><input type="text" id="txt"></form><p>Click on the button above. The input field will count for ever, starting at 0.</p></body>
function stopCount(){clearTimeout(t);}</script></head>
<body><form><input type="button" value="Start count!" onClick="timedCount()"><input type="text" id="txt"><input type="button" value="Stop count!" onClick="stopCount()"></form><p>Click on the "Start count!" button above to start the timer. The input field will count forever, starting at 0. Click on the "Stop count!" button to stop the counting.</p></body>
</html>
<html><head><script type="text/javascript">function startTime(){var today=new Date();var h=today.getHours();var m=today.getMinutes();var s=today.getSeconds();// add a zero in front of numbers<10m=checkTime(m);s=checkTime(s);document.getElementById('txt').innerHTML=h+":"+m+":"+s;t=setTimeout('startTime()',500);}
function checkTime(i){if (i<10) { i="0" + i; }return i;}</script></head>
var str="Visit Microsoft!";document.write(str.replace(/Microsoft/,"W3Schools"));
</script></body></html>
<html><body>
<script type="text/javascript">
document.write(Date());
</script>
</body></html>
<html><body>
<script type="text/javascript">
var minutes = 1000*60;var hours = minutes*60;var days = hours*24;var years = days*365;var d = new Date();var t = d.getTime();var y = t/years;
document.write("It's been: " + y + " years since 1970/01/01!");
</script>
</body></html>
<html><body>
<script type="text/javascript">
var d = new Date();d.setFullYear(1992,10,3);document.write(d);
</script>
</body></html>
<html><body>
<script type="text/javascript">
var d = new Date();document.write (d.toUTCString());
</script>
</body></html>
<html><body>
<script type="text/javascript">
var d=new Date();var weekday=new Array(7);weekday[0]="Sunday";weekday[1]="Monday";weekday[2]="Tuesday";weekday[3]="Wednesday";weekday[4]="Thursday";weekday[5]="Friday";weekday[6]="Saturday";
document.write("Today it is " + weekday[d.getDay()]);
</script>
</body></html>
<html><head><script type="text/javascript">function startTime(){var today=new Date();var h=today.getHours();var m=today.getMinutes();var s=today.getSeconds();// add a zero in front of numbers<10m=checkTime(m);s=checkTime(s);document.getElementById('txt').innerHTML=h+":"+m+":"+s;t=setTimeout('startTime()',500);}
function checkTime(i){if (i<10) { i="0" + i; }return i;}</script></head>
<body><p></p><b>Insert a number into one of the input fields below:</b></p><form><input id="c" name="c" onkeyup="convert('C')"> degrees Celsius<br />equals<br /> <input id="f" name="f" onkeyup="convert('F')"> degrees Fahrenheit </form><p>Note that the <b>Math.round()</b> method is used, so that the result will be returned as an integer.</p></body>
<body><a id="myAnchor" href="http://www.microsoft.com">Visit Microsoft</a><input type="button" onclick="changeLink()" value="Change link"><p>In this example we change the text and the URL of a hyperlink. We also change the target attribute.The target attribute is by default set to "_self", which means that the link will open in the same window.By setting the target attribute to "_blank", the link will open in a new window.</p></body>
<p><a id="w3" href="http://www.w3schools.com/">W3Schools.com</a> (Use Alt + w to give focus to the link)</p><p><a id="w3dom" href="http://www.w3schools.com/htmldom/">HTML DOM</a> (Use Alt + d to give focus to the link)</p>
<body><input name="myInput" type="text" size="20" /><br /><input name="myInput" type="text" size="20" /><br /><input name="myInput" type="text" size="20" /><br /><br /><input type="button" onclick="getElements()" value="How many elements named 'myInput'?" /></body>
</html>
<html><head><script type="text/javascript">function createNewDoc(){var newDoc=document.open("text/html","replace");var txt="<html><body>Learning about the DOM is FUN!</body></html>";newDoc.write(txt);newDoc.close();}</script></head>
<body><input type="button" value="Open and write to a new document" onclick="createNewDoc()"></body>
<p>To access an item in a collection you can either use the number or the name of the item:</p>
<script type="text/javascript">document.write("<p>The first form's name is: " + document.forms[0].name + "</p>");document.write("<p>The first form's name is: " + document.getElementById("Form1").name + "</p>");</script>
<html><head><script type="text/javascript">function whichButton(event){if (event.button==2) { alert("You clicked the right mouse button!"); }else { alert("You clicked the left mouse button!"); }}</script></head>
<body onmousedown="whichButton(event)"><p>Click in the document. An alert box will alert which mouse button you clicked.</p></body>
</html>
<html><head><script type="text/javascript">function show_coords(event){x=event.clientX;y=event.clientY;alert("X coords: " + x + ", Y coords: " + y);}</script></head>
<body onmousedown="show_coords(event)">
<p>Click in the document. An alert box will alert the x and y coordinates of the mouse pointer.</p>
<body onkeyup="whichButton(event)"><p><b>Note:</b> Make sure the right frame has focus when trying this example!</p><p>Press a key on your keyboard. An alert box will alert the unicode of the key pressed.</p></body>
</html>
<html><head>
<script type="text/javascript">function coordinates(event){x=event.screenX;y=event.screenY;alert("X=" + x + " Y=" + y);}
var age=document.getElementById("age").value;var fname=document.getElementById("fname").value;submitOK="true";
if (fname.length>10) { alert("The name must be less than 10 characters"); submitOK="false"; }if (isNaN(age)||age<1||age>100) { alert("The age must be a number between 1 and 100"); submitOK="false"; }if (at==-1) { alert("Not a valid e-mail!"); submitOK="false"; }if (submitOK=="false") { return false; }}</script></head>
<body><form><input size="25" type="text" id="myText" value="A cat played with a ball"><input type="button" value="Select text" onclick="selText()"> </form></body>
<body><iframe src="frame_a.htm" id="frame1"></iframe><iframe src="frame_b.htm" id="frame2"></iframe><br /><br /><input type="button" onclick="changeSource()" value="Change source of the two iframes">
<body><iframe src="frame_a.htm" id="frame1"></iframe><iframe src="frame_b.htm" id="frame2"></iframe><br /><br /><input type="button" onclick="changeSource()" value="Change source of the two iframes">
<body><h3>Links and Anchors</h3><p>Click on a button to display that anchor in a second window!</p><input type="button" value="0" onclick="linkTo(0)"><input type="button" value="1" onclick="linkTo(1)"><input type="button" value="2" onclick="linkTo(2)"><input type="button" value="3" onclick="linkTo(3)"></body>
<html><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("Your browser is good enough!"); }else { alert("It's time to upgrade your browser!"); }}</script></head>
<form><select id="mySelect"> <option>Apple</option> <option>Pear</option> <option>Banana</option> <option>Orange</option></select><input type="button" onclick="getLength()" value="How many options in the list?"></form>
</select><input type="button" onclick="selectMultiple()" value="Select multiple"></form><p>Before you click on the "Select multiple" button, try to select more than one option (by holding down the Shift or Ctrl key). Then click on the "Select multiple" button and try again.</p>
<table width="50%" border="1"><tr id="tr1"><th>Firstname</th><th>Lastname</th><th>Text</th></tr><tr id="tr2"><td>Peter</td><td>Griffin</td><td>Hello my name is Peter Griffin. I need a long text for this example. I need a long text for this example.</td></tr></table><br /><input type="button" onclick="topAlign()" value="Top-align table row" />
<html><head><script type="text/javascript">function disp_alert(){alert("Hello again! This is how we" + '\n' + "add line breaks to an alert box!");}</script></head><body>
<input type="button" onclick="disp_confirm()" value="Display a confirm box" />
</body></html>
<html><head><script type="text/javascript">function disp_prompt(){var name=prompt("Please enter your name","Harry Potter");if (name!=null && name!="") { document.write("Hello " + name + "! How are you today?"); }}</script></head><body>
<input type="button" onclick="disp_prompt()" value="Display a prompt box" />
<body><form><input type="button" onclick="resizeWindow()" value="Resize window"></form><p><b>Note:</b> We have used the <b>top</b> element instead of the <b>window</b> element, to represent the top frame. If you do not use frames, use the <b>window</b> element instead.</p></body>
<body><form><input type="button" onclick="resizeWindow()" value="Resize window"></form><p><b>Note:</b> We have used the <b>top</b> element instead of the <b>window</b> element, to represent the top frame. If you do not use frames, use the <b>window</b> element instead.</p></body>
<body><form><input type="button" value="Display timed alertbox!" onClick = "timedMsg()"></form><p>Click on the button above. An alert box will be displayed after 5 seconds.</p></body>
<input type="button" value="Display timed text!" onClick="timedText()"><input type="text" id="txt"></form><p>Click on the button above. The input field will tell you when two, four, and six seconds have passed.</p></body>
<body><form><input type="button" value="Start count!" onClick="timedCount()"><input type="text" id="txt"></form><p>Click on the button above. The input field will count for ever, starting at 0.</p></body>
function stopCount(){clearTimeout(t);}</script></head>
<body><form><input type="button" value="Start count!" onClick="timedCount()"><input type="text" id="txt"><input type="button" value="Stop count!" onClick="stopCount()"></form><p>Click on the "Start count!" button above to start the timer. The input field will count forever, starting at 0. Click on the "Stop count!" button to stop the counting.</p></body>
</html>
<html><head><script type="text/javascript">function startTime(){var today=new Date();var h=today.getHours();var m=today.getMinutes();var s=today.getSeconds();// add a zero in front of numbers<10m=checkTime(m);s=checkTime(s);document.getElementById('txt').innerHTML=h+":"+m+":"+s;t=setTimeout('startTime()',500);}
function checkTime(i){if (i<10) { i="0" + i; }return i;}</script></head>
<html><head><script type="text/javascript">function show_popup(){var p=window.createPopup();var pbody=p.document.body;pbody.style.backgroundColor="lime";pbody.style.border="solid black 1px";pbody.innerHTML="This is a pop-up! Click outside the pop-up to close.";p.show(150,150,200,50,document.body);}</script></head>