CS185c
Chris Pollett
Feb. 20, 2010
<script type="text/javascript"> <!-- hide from old browsers -- code -- // --> </script>
<script type="text/javascript" src="myscript.js" />
return x; // has the effect of just return!
x = 5.5; y = x.toString();
document.write("Hi there!"); document.writeln("This strings has a new line after it!");
<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_object.make= "V6" /* would then give a property make a value. */ //can access as p = my_object["make"] q = my_object.make
my_object.subObject = new Object();
for(var prop in my_object){...}
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 }
Which of the following is true?
function car(new_make, new_model, new_year) { this.make = new_make; this.model = new_model; this.year = new_year; }
my_car = new car("Ford", "Contour SVT", "2000");
function display_car() { document.write("Make:", this.make, "<br />"); document.write("Model:", this.model, "<br />"); document.write("Year:", this.year, "<br />"); } function car(/*same as before*/) { //same as before this.display = display_car;}
function car(/*same as before*/) { //same as before} car.prototype.display = display_car;
document.getElementById("yo").onclick = function() {alert("yo");}in jQuery we could do: $("#yo").onclick = function() {alert("yo");}
$(document).ready(handler) // or $(handler)
<!DOCTYPE html> <html> <head> <title>Portrait or Landscape</title> <meta id="myview" name="viewport" content="width=device-width, initial-scale=1" /> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" /> <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script> <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script> <script> $(function(){ // Orientation Change // When orientation changes event is triggered // exposing an orientation property of either // landscape or portrait $('body').bind('orientationchange', function(event){ changeOrientation(event.orientation) }) // Change the style depending on orientation function changeOrientation(ori) { // Remove all classes $("#orientation").removeClass('portrait landscape'); $("#orientation").addClass(ori); $("#orientation").html("<p>"+ori.toUpperCase()+"</p>"); } $("body").trigger("orientationchange"); }) </script> <style> div.portrait { background-color: blue; width: 100%; height: 100%; } div.landscape { background-color: red; width: 100%; height: 100%; } p { font-size:24px; color:white; } </style> </head> <body> <div data-role="page"> <div data-role="header"> <h1>Orientation</h1> </div><!-- /header --> <div data-role="content"> <div id="orientation" class="portrait" ></div> </div><!-- /content --> <div data-role="footer"> <h4>My Footer</h4> </div><!-- /footer --> </div><!-- /page --> </body> </html>