JavaScript Cookbook
Basic Structure
<SCRIPT language="JavaScript">
The script itself
</SCRIPT>
|
May be before (onload) or after </HTML> (onunload), in <HEAD> (functions group) or in <BODY> (when needed).
or online : <A href="javascript:alert('Hello!')">How are you ?</A>
or from a file : <script src="myfile.js"></script>
Javascript Syntax
Instruction Separator :
|
;
|
Comment line :
|
// Comment
|
Function Declaration :
|
function function_name(Variables) { function_body ... return result }
|
Condition 1 :
|
if (condition) { expression1 } else { expression2 }
|
Condition 2 :
|
while (condition) { expression }
|
Loop :
|
for (Initialisation;Ending Condition;Step Command) { ... }
|
Some system functions
Get the current date :
|
var date=new Date()
|
Convert date into time type :
|
var time=date.getTime() (1000th of second)
|
Convert time into date type :
|
date.setTime(time) (mm/dd/yyyy hh:mm:ss)
|
Get day from date :
|
date.getDay()
|
Get month from date :
|
date.getMonth()
|
Get year from date :
|
date.getYear()
|
Get hours from date :
|
date.getHours()
|
Get minutes from date :
|
date.getMinutes()
|
Get seconds from date :
|
date.getSeconds()
|
Convert date into string type :
|
string=date.toLocaleString()
|
Ask for a prompt :
|
answer=prompt(question_string,default_value) (= NULL if Cancel)
|
Open an Alert window :
|
alert(string) (Buttons: OK)
|
Open a Confirm window :
|
answer=confirm(string) (Buttons: OK and Cancel)
|
Return the width of current window :
|
screen.width
|
Return the height of current window :
|
screen.height
|
Return the number of colors :
|
screen.colorDepth (in Bytes)
|
Return the name of the explorer :
|
navigator.appName
|
Return the version of the explorer :
|
navigator.appVersion
|
Return the reference of the document :
|
document.referrer
|
Number of letters before 1st caracter :
|
string.indexOf(caracter)
|
Number of letters before last caracter :
|
string.lastIndexOf(caracter)
|
Return the value of a form input :
|
input.value
|
Status value of a window (link) :
|
window.status='status_appearing_in_the_lower_bar'
|
Return the lower integer value of real :
|
Math.floor(real)
|
Some system processes
Display a string type :
|
adr.document.write(string) (string can be any "text and HTML marks", and adr the window address)
|
Open a window :
|
var adr=window.open(url,"","width="+width+",height="+height+",left="+left+",top="+up)
|
Close the current window :
|
window.close()
|
Set the cursor in the form input :
|
input.focus()
|
Set a script action with a timeout :
|
setTimeout(script, timeout)
|
Some (usefull ?) Javascripts
48741 visitors
850589 robots
since 01/01/2003
Page generated
in 1.36 seconds
💗 2003-2024 by S. MARLIERE. Copying is an act of love. Love is not subject to law. Please copy and share.