
<script type="text/javascript">
alert('Hello')
</script>
<script type="text/javascript" src="myscript.js" />
return x; // has the effect of just return!
x = 5.5; y = x.toString()
<a href="javascript:alert('hi there')">Hi there alert</a>
Hi there alert
<button onclick="alert('hi there')">Hi There!</button>
<button id="yo">Yo!</button>
<script>
document.getElementById("yo").onclick = function() {alert("yo")}
</script>
var my_object = new Object()
my_car.make= "Honda" /* would then give a property make a value. */ //can access as p = my_car["make"] q = my_car.make
my_car.owner = { name: 'Fred', age: 42 }
for (var prop in my_car){...}
var myArr = new Array(1, 2, "hello") var myArr = new Array(100) var myArr = [1,2,3] //to access myArr[0] //to determine length myArr.length
Below is an example of the syntax for declaring a function in Javascript
function swap(i, j, a)
{
var tmp=a[i] /* explicitly defined variables
have scope within the function
if I had declared the variable
implicitly it would have global scope */
a[i] = a[j]; a[j] = tmp
}
swap(10, 5, b)
var b = swap
swap = function(i, j, a) { /* code above */}
function swap()
{ var i = this.arguments[0], j=this.arguments[1], a=this.arguments[2]
//same code as before
}
document.getElementById("yo").onclick = function() {alert("yo")} in jQuery we could do:
$("#yo").onclick = function() {alert("yo")}
$(document).ready(handler)

var question = { text: '...', choices: ['...', '...', '...', '...'], answer: '...' }
(Eventually, we'd want to get that object from the internet.) We want to set the UI programmatically. That needs to be done in a callback when the document has been loaded. Add the following into the head part of your page:
<script type="text/javascript">
$(document).ready(function() {
$('a').click(function(event) { alert(event.target.firstChild.textContent) })
})
</script> Next, remove the href attributes from the links Now what happens when you click a link?
var question object to the top of the script. Remove the hard-coded header and add this to the ready handler:
$('#questionText').html(question.text) What happens? Why?
<li> tags from the <ul> element, and put them in programmatically with this outline:
var choices = $('#choices')
for (var i = 0; i < question.choices.length; i++) {
var item = $('<li/>')
var link = $('<a/>').html(question.choices[i])
item.append(link)
choices.append(item)
}
choices.listview('refresh') What happens when you run it?
for loop):
link.click(function() {
if (...)
$.mobile.changePage('#correct')
else
...
} Note that in the function, you can access the enclosing variables such as i and question.