CS1315: Introduction To Media Computation
CS1315: Introduction To Media Computation
Introduction to
Media Computation
“What do other languages look like?
How hard is it to do Web
programming?”:
JavaScript
What do other languages look like?
We call the language “look” its syntax
Python is a fairly traditional language in terms of
syntax.
Languages like Scheme and Squeak are
significantly different.
Major points of difference:
Whether or not variables have to be declared before
first use.
Details of how individual lines are written.
Details of how blocks are defined.
JavaScript
JavaScript is meant to be a scripting language, like
Python.
Scripting languages are meant for non-professional
programmers to solve simple tasks.
It’s designed to look like Java to ease the transition in
either way.
JavaScript can be used by the web server (used on the
computer accessed via the Internet), or it can be used
within an HTML page.
If it’s within the HTML page, it’s actually executed by the
user’s browser.
We call that client side JavaScript.
JavaScript syntax: Variables
Variables must be declared before use.
You can’t just say:
a = 12
You can either say:
var a = 12;
Or:
var a;
a = 12
In other languages, you might also declare the
variable’s type
int a=12;
JavaScript syntax: Blocks
Blocks are delimited with curly braces.
function test()
{
document.writeln("This is a test");
}
JavaScript syntax: Individual
statements
Lots of differences:
func tion instead of def
End lines with semicolons “;”
(But lines can have returns in the middle of them.)
The for statement is numeric (mostly) and has
different parts to it.
You use write or writeln instead of pri nt
But they’re mostly detail changes.
The basic operation of JavaScript is not unlike
Python.
JavaScript is all about objects
Just about every function is actually a method.
For example, there is no global print.
There is a function write or writeln
Writeln adds a new line (‘\n’) at the end.
But these aren’t global functions.
To write into the document, you use
document. wri te()
document. wri te() is a method on the HTML
document itself.
Embedding JavaScript inside HTML
Sure!
Anything you can compute.
Anything that you can get from builtin functions
(mostly methods).
There are lots of them.
You don’t have to have a function either.
You can just put the script in-line
Displaying the date and time
<p>This is a very simple web
page.</p>
<p><image
src="mediasources/barbara.jpg
" />
</p>
<p>This is being served to you on
<script>document.write(Date()
);
</script></p>
Using dialogs in JavaScript
function check()
{
var agree = false;
agree = confirm('Do you enjoy CS?');
if (agree)
notes=prompt("Give me one good thing about CS:"); agree will be true or
if (! agree) false.
notes=prompt("Why don't you like CS?");
alert("You said:"+notes); ! agree is not agree.
}
…
<script> check() </script>
</body> Notice: We can indent or not
</html> indent as we want here.
Indentation is not important
in JavaScript (or most other
languages.)
What happens when this runs
Different kinds of dialogs
Confirm: Puts up a prompt, returns true or false.
Alert: Beeps and displays one thing with an OK
button. No return.
Prompt: Asks the user for a line of text. Returns
that text.
Running on Loading the Page
This program runs when the page loads.
Is that what you really want to happen?
The user sees nothing at all until they go to your
page and then these dialog boxes happen.
Isn’t it more natural for dialog boxes to pop up
when you click on something?
Events: Key to responding to users
</body>
Opening another Window
To open another window, we use the function
open()
Open can take up to three inputs:
First input is the URL to open.
Second is the name of the window
Third is a string with a variety of inputs possible.
Popping up a window on a click
<html>
<head>
<title>The Simplest Possible Web Page</title>
<script>
function goToHawaii()
{
var win=open('http://www.cc.gatech.edu/~mark.guzdial/hawaii/','Hawaii');
}
</script>
</head>
<body>
<h1>A Simple Heading</h1>
<p>This is a very simple web page.</p>
<p><image src="mediasources/beach.jpg" onClick="goToHawaii()" />
This page was created on <script> document.write(Date()); </script></p>
</body>
</html>
Popping up a Window
Changing the window’s
characteristics
<head>
<title>The Simplest Possible Web Page</title>
<script>
function goToHawaii()
{
var
win=open('http://www.cc.gatech.edu/~mark.guzdial/hawaii/','Hawaii',
"titlebar=no,width=200");
}
</script>
</head>
Changing the window’s
characteristics
Controlling colors with mouseOver
and mouseOut
<body>
<h1>A Simple Heading</h1>
<p>This is a very simple web page.</p>
<p>Pick any item...</p>
<ul>
<li onmouseover="this.style.color='green'"
onmouseout="this.style.color='black'">Pick me!</li>
<li onmouseover="this.style.color='red'"
onmouseout="this.style.color='yellow'">No, pick me!</li>
<li onmouseover="this.style.color='magenta'"
onmouseout="this.style.color='pink'">No, no -- I'm the one!</li>
</ul>
Fields and Buttons in HTML
To create fields and buttons in HTML, we need a
form.
Forms are delimited with <form> and </form>
Examples of things we can have in forms.
<input type=“text” name=“afield1”>
<input type=“button” value=“Click me”>
Type=“textarea” is for more than one line of text.
Like the CoWeb edit text.
Type=“radio” is for radiobuttons.
Simple Form
<html>
<head>
<title>Simplest Form in HTML</title>
</head>
<body>
<h1>A Simple Heading</h1>
<p>This is a very simple web page.</p>
<form>
<input type="text" name="afield">
<input type="button" value="Click me">
</form>
</body>
</html>
Forms and CGI Scripts
Forms can also point to particular URLs
Form URLs are typically CGI Scripts
CGI Scripts are programs (written in Perl or Python)
that will process the form, which will be passed in
as a parameter.
We can also do processing of form input
completely from within JavaScript.
Inches/Centimeter Converter
<body>
<h1>Inches/Centimeter
Converter</h1>
<form>
<p>Centimeters:<input type="text"
name="cm"
onchange="this.form.inches.value=th
is.value * 2.54"></p>
<p>Inches:<input type="text"
name="inches"
onchange="this.form.cm.value =
this.value / 2.54"></p>
</form>
</body>
Doing Multimedia in JavaScript
It’s possible to do multimedia in JavaScript, but
it’s not like in Python.
We can’t control pixels or samples.
Most common way to do JavaScript is through
plugins.
Like Apple QuickTime, RealVideo, Netscape
LiveAudio
Can do some simple animations from JavaScript.
Animated Motion in JavaScript
There is a setInterval() function that can make a
JavaScript function run at regular intervals.
We use that to schedule motion to occur.
Divisions (<div></div>) can be controlled with
styles, that can have positions.
We then make a function to adjust the position of
the division.
Animated Motion in JavaScript
<html> </head>
<head> <body
<title>The Simplest Possible Web ONLOAD="setInterval('drift()',100)"
Page</title> >
<style> <h1>A Simple Heading</h1>
#barb { position: absolute; left:0; top: <p>This is a very simple web page.</p>
0; } <div id="barb">
</style> <p><image
<script> src="mediasources/barbara.jpg" />
function drift() </p>
{ </div>
var object = document.all.barb.style; <p>Here is some more text, just to make
the document larger and make
object.pixelTop = object.pixelTop + 5;
object.pixelLeft = object.pixelLeft + 5; it obvious where the picture is drifting.
</body>
}
</script>
</html>
Why JavaScript?
To do simple processing from within HTML.
To control plugins.
JavaScript vs. Python
JavaScript’s syntax is much like other
programming languages.
JavaScript can’t do everything that Python can.
Python is a more full-featured programming
language.
But Python can’t be emedded inside of HTML.