- JavaScript is used to define the behaviour of webpages.
- It makes your site interactive.
- You send and receive information without refreshing the page.
###jQuery
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<script>//stuff</script><!-- or --><scriptsrc="script.js"></script>REFERENCELiterals:10000.1Strings:"John Citizen"'a'
Strings are just pieces of text
Variables:x = 5name = "Kate"
Variables store values to be accessed later
Operators and Expressions:+ - * /5 * 2 = 10"Dog" + " " + "Cat"
Expressions manipulate values
Comments:
Comments exist after // or between /* and */
Comments aren't run by your browser
Write a program that asks for a user's age, and tell them how old they were in a given year
varage=parseInt(prompt("What is your age?"))vardate=newDate().getFullYear();vardifference=parseInt(prompt("What year do you want to know how old you were on this date?"))alert(difference-date+age)In programming, we like to check whether things are true or false
1==1true1!="1"true"hello"=='hello'true1==2falseWe also often have to store multiple variables in succession
varnames=["Jack","Jill","Mary","Sue"]Which we can access later
names[0]"Jack"names[3]"Sue"names.length4names[names.length]We often only want to perform actions if certain conditions are met
varpassword=prompt("Password:")if(password=="qwertyuiop"){alert("Welcome")}elseif(password=="poiuytrewq"){alert("You are denied access")}else{alert("Incorrect Password")}varguessedPassword=falsewhile(!guessedPassword){varguess=prompt("Password:")guessedPassword=(guess=="franklin")}alert("Welcome")We can use for loops to iterate over arrays
varnames=["Jack","Jill","Mary","Sue"]varages=[22,31,43,65]for(vari=0;i<names.length;i++){alert(names[i]+": "+ages[i].toString())}Functions define code that we want to use more than once
functionguessedPassword(){varguessedPassword=falsewhile(!guessedPassword){varguess=prompt("Password:")guessedPassword=(guess=="franklin")}alert("Welcome")}guessedPassword()When certain things happen in HTML, we can call JavaScript functions DEMO