Pierwszy program My first JavaScript! <br> To jest normalny dokument HTML. <br> document.write("to jest JavaScript!") <br> I znowu dokument HTML. Zmienne var name = "Ja" document.write(name) document.write("<h1>"+name+"</h1>") if var d = new Date() var time = d.gethours() if (time < 10) document.write("<b>good morning</b>") <p> jeśli jest przed 10 to zobaczysz "Good morning". </p> else1 var d = new Date()
var time = d.gethours() if (time < 10) document.write("<b>good morning</b>") else document.write("<b>good day</b>") else2 var d = new Date() var time = d.gethours() if (time<10) document.write("<b>good morning</b>") else if (time>=10 && time<16) document.write("<b>good day</b>") else document.write("<b>hello!</b>") switch var d = new Date() theday=d.getday() switch (theday) case 5: document.write("<b>finally Friday</b>") break case 6: document.write("<b>super Saturday</b>") break case 0: document.write("<b>sleepy Sunday</b>") break
default: document.write("<b>i'm really looking forward to this weekend!</b>") <p>this JavaScript will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc.</p> for1 for (i = 0; i <= 5; i++) document.write("the number is " + i) document.write("<br />") <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> for2 for (i = 1; i <= 6; i++) document.write("<h" + i + ">This is header " + i) document.write("</h" + i + ">") for3 var i=0 for (i=0;i<=10;i++) if (i==3)break document.write("the number is " + i) document.write("<br />") <p>explanation: The loop will break when i=3.</p>
while1 i = 0 while (i <= 5) document.write("the number is " + i) document.write("<br>") i++ <p>explanation:</p> <p><b>i</b> 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> while2 i = 0 do document.write("the number is " + i) document.write("<br>") i++ while (i <= 5) <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> alert1 function disp_alert() alert("i am an alert box!!")
<input type="button" onclick="disp_alert()" value="display alert box"> alert2 function disp_alert() alert("hello again! This is how we" + '\n' + "add line breaks to an alert box!") <input type="button" onclick="disp_alert()" value="display alert box"> alert3 <!-- hide script from old browsers function getname(str) alert("hi, "+ str+"!"); // end hiding contents --> Wpisz swoje imię: <input type="text" name="name" onblur="getname(this.value)" value=""> alert4 function myfunction() alert("hello")
<input type="button" onclick="myfunction()" value="call function"> <p>by pressing the button, a function will be called. The function will alert a message.</p> alert5 function myfunction(txt) alert(txt) <input type="button" onclick="myfunction('hello')" value="call function"> <p>by pressing the button, a function with an argument will be called. The function will alert this argument.</p> alert6 function myfunction(txt) alert(txt) <input type="button" onclick="myfunction('good Morning!')" value="in the Morning"> <input type="button" onclick="myfunction('good Evening!')" value="in the Evening">
<p> When you click on one of the buttons, a function will be called. The function will alert the argument that is passed to it. </p> confirm function disp_confirm() var name=confirm("press a button") if (name==true) document.write("you pressed the OK button!") else document.write("you pressed the Cancel button!") <input type="button" onclick="disp_confirm()" value="display a confirm box"> promp function disp_prompt() var name=prompt("please enter your name","") if (name!=null && name!="") document.write("hello " + name + "! How are you today?") <input type="button" onclick="disp_prompt()" value="display a prompt box">
funkcja1 function myfunction() return ("Hello, have a nice day!") document.write(myfunction()) <p>the script in the body section calls a function.</p> <p>the function returns a text.</p> funkcja2 function product(a,b) return a*b document.write(product(4,3)) <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> tablice var x var mycars = new Array() mycars[0] = "Saab" mycars[1] = "Volvo" mycars[2] = "BMW" for (x in mycars) document.write(mycars[x] + "<br />")
onerror onerror=handleerr var txt="" 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!") <input type="button" value="view message" onclick="message()" />
onclick function pushbutton() alert("hello!"); <input type="button" name="button1" value="naciśnij mnie" onclick="pushbutton()"> data1 Ostatnia modyfikacja: document.write(document.lastmodified) data2 today = new Date() document.write("the time now is: ",today.gethours(),":",today.getminutes()) document.write(" The date is: ", today.getmonth()+1,"/",today.getdate(),"/",today.getyear()); onmouseover function hello() alert("hello!");
<a href="" onmouseover="hello()">link</a> Tworzenie okien function WinOpen() msg=open("","displaywindow","toolbar=no,directories=no,menubar=no"); msg.document.write("<head><title>yo!</title></head>"); msg.document.write("<center><h1><b>to jest naprawdę fajne! </B></h1></CENTER>"); <input type="button" name="button1" value="naciśnij mnie" onclick="winopen()"> formularz1 function getname(str) alert("hi, "+ str+"!"); Wpisz swoje imię i kliknij enter: <input type="text" name="name" onblur="getname(this.value)" value=""> formularz2 function test1(form) if (form.text1.value == "") alert("podaj ciąg znaków!") else alert("cześć "+form.text1.value+"! Informacja poprawna!");
function test2(form) if (form.text2.value == "" form.text2.value.indexof('@', 0) == -1) alert("niepoprawny adres poczty elektronicznej!"); else alert("ok!"); <form name="first"> Wpisz swoje nazwisko:<br> <input type="text" name="text1"> <input type="button" name="button1" value="test" onclick="test1(this.form)"> <P> Wpisz adres swojej poczty elektronicznej:<br> <input type="text" name="text2"> <input type="button" name="button2" value="test" onclick="test2(this.form)"> formularz3 <FORM NAME="buttonbar"> <INPUT TYPE="button" VALUE="Back" onclick="history.back()"> <INPUT TYPE="button" VALUE="Home" onclick="location='script.htm'"> <INPUT TYPE="button" VALUE="Next" onclick="history.forward()"> </FORM> Możesz także napisać history.go(-1) i history.go(1).