JavaScript Exercise 1a
Background Resources
- Core JavaScript Guide version 1.5
- The canonical reference guide to the JavaScript language. This is the recommended text for this part of the course.
- JavaScript and Web Forms
- all you need to know about JavaScript and Web Forms.
- JavaScript Examples 1
- Examples that come from a JavaScript book
- More JavaScript Examples 1
- Other examples but watch out for the interspersed software adverts!
- The JavaScript Shell
- An interactive JavaScript interpreter for Firefox.
Practicing JavaScript
Look at the JavaScript guide and examples above. Use the JavaScript shell to experiment with the language. Try out function definitions and array manipulations. Have a look at the builtin objects that the browser exposes: window and document. Try the following:
- 4*6
- r=1/3
- s=Math.sqrt(10);
- function fac(n){if(n<=1)return 1;else return n*fac(n-1)}
- fac(4)
- fac
- twice=function(n){return n+n}
- twice(4)
- twice("4");
- arr=[0,1,2,3,4];
- arr[5]=5;
- arr[0]=9
- arr
- for(i in arr)arr[i]=i*i;
- for(i=0; i<arr.length; i++)arr[i]=i*i; //same as previous
- arr
- s="Hello COMP3001";
- s[0]
- marks={comp3016: 64, comp3001: 75, comp1003: 49}
- marks.comp3016
- marks["comp3001"]
- alert("Hello there: "+new Date());
- "Hello there".substring(4);
- window.resizeTo(300,200)
- window.moveTo(100,100)
- w=window.open() //may have to do this twice if popupblocker stops you
- d=w.document
- d.write("<h1>b lah</h1><p>text</p>")
- d.documentElement
- d.documentElement.firstChild
- d.documentElement.firstChild.firstChild.nextSibling
- d.documentElement.firstChild.firstChild.nextSibling.innerHTML
- for(i in window)print(i);
- for(i in d)print(i+"->"+ d[i]);
- Create a shell bookmarklet by dragging this link to your bookmark bar.
Then open the forms example in a new window.
Then open the shell bookmarlet from that page and type the following expressions into the shell to explore JavaScript's control of forms.
- document.forms[0];
- document.forms[0].elements[0];
- document.forms[0].elements[0].value="Hello";
- document.example.address.value="134, Ringwood Rd";
- if(document.example.email.value.indexOf("@")<0)alert("Bad email");