Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword or section
Like this
19Activity
P. 1
Java Tutorial

Java Tutorial

Ratings: (0)|Views: 216|Likes:
Published by forlon
This is second part of what is JavaScript.
This is second part of what is JavaScript.

More info:

Published by: forlon on Jan 12, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

10/22/2011

pdf

text

original

 
Introduction
This course deals with Scripts. A Script is a segment of code that manipulates the browser and its contents inways that is not possible with ordinary HTML or Cascading Style Sheets. By using a script in your web pages,you can gain more control of how the page looks and behaves: dates and times can be added to the page, formelements validated before the contents are sent, browser details checked, cookies set, even simple games can beadded to a web page - all with a scripting language.The learning curve for scripting is a lot a steeper than HTML and Style Sheets. But you can learn the basics,and use scripts on your own pages, without it causing you too much trouble. The scripting language covered inthese pages is meant to get you started on the subject, and is not intended as an in-depth study.We're going to study the JavaScript programming language, because it is a widely-used scripting language for web pages. All the scripts in these pages have been tested with modern versions of a wide variety of browsers.If you're ready, then, let's make a start.A First ScriptLet's jump right in with a bit of code.. With your editor open, copy the following code. When you're donecopying it, save your work and load it into your browser.<HTML><HEAD><TITLE>A First Script</TITLE></HEAD><BODY>
<SCRIPT LANGUAGE = JavaScript>document.write("Hello World")</SCRIPT>
</BODY></HTML>All right, how did you get on? All that typing should have gotten you this in the browser:
"Hello World"
Granted, that's a heck of a lot of trouble to go to just to write "Hello World". But it's a start. Let's explain what'sgoing on.
1
 
When you're writing your scripts, you enclose them between two <SCRIPT> tags, an opening one and a closingone. The opening one should tell the browser what language the script is written in:
<SCRIPT LANGUAGE = JavaScript>
The closing Script tag is just the word SCRIPT in between two angle brackets with a forward slash:
</SCRIPT>
Most of your JavaScript will go between these two tags. So what's all that "document dot write" bit?
document.write("Hello World")
Document is part of something called the Document Object Model. Document refers to all the text and HTMLelements between the two BODY tags. And that includes any attributes inside the BODY tag itself. LikeBGCOLOR.Write( ) is a method of Document. A method is a bit of code that actually does something. As opposed to aProperty, which IS something. Methods are usually Verbs, and Properties usually Nouns. The Write( ) methodwrites text (and numbers as well) between the two BODY tags on your page.For all you English language experts out there who might be protesting about the lack of capital letters,Document is spelt with a lowercase "d", and Write with a lowercase "w". Try changing your code to this andsee what happens:
Document.Write("Hello World")
JavaScript is damned picky about capital letters - it doesn't like them at all!The part or parts between the two brackets of write( ) are what will be written to your page. Direct text goes between two double quotes; Variables don't need any. Whoops, we haven't done variables yet. We'll get tothem.So the whole line reads "Write the text Hello World between the two BODY tags of the web page."Don't worry if you don't understand some of that - the main point is that you are up and running, and you'vewritten your first script. The journey has just started.
Where to put the Script Tags
The Script Tag and HTML
At the moment, we have our script between the two BODY tags. And it works perfectly well here. It's quitehappy where it is. However, SCRIPTS are best kept in the HEAD section of your HTML. This is because anycode in the HEAD section will be dealt with first by the browser. And besides, it's neater up there. You're notcluttering up your HTML code with lots of JavaScript.
2
 
So, cut your script out of the BODY section, and paste it into the HEAD section. Like this:<HTML><HEAD><TITLE>A First Script</TITLE>
<SCRIPT LANGUAGE = JavaScript>document.write("Hello World")</SCRIPT>
</HEAD><BODY></BODY></HTML>Save your work, and then view the results in your browser. Did it make a difference? No, it did not. But restassured that your script was dealt with before anything in the BODY section.You can also put your scripts into HTML tags. Here's the document.write() code inserted into a Form's Buttoncode:<BODY><INPUT TYPE = Button VALUE = "Click Me"OnClick = "document.write('Hello World')"></BODY>Looks a bit messy, but then scripting languages can get like that. Notice, however, that we've shifted thedocument code to our button:
OnClick = "document.write('Hello World')"
OnClick is an event that can be applied to buttons (amongst other things.) We'll get to Events later, but for now,note that the same code we wrote earlier then goes after an equals sign ( = ). Or is it the same code? Have youspotted the difference?Yes, Hello World is now in single quotes! That's because we used up our double quotes surrounding thedocument.write() part. And JavaScript doesn't like you using two sets of double quotes in the same bit of code.There's a lot of things that JavaScript doesn't like. Get used to it.So what have we learnt so far? We've learnt this:
Scripting code is written between a pair of 
<SCRIPT> </SCRIPT>
tags
You can refer to the two BODY tags by using the word "
document
"
You can use the
write( )
method of document to insert stuff onto your web pages
JavaScript is very picky about the way you spell things
3

Activity (19)

You've already reviewed this. Edit your review.
1 hundred reads
rajesh patel liked this
kafu46 liked this
milal04 liked this
alex.embsys liked this
rickco liked this
aryaghosh1 liked this
wellbablu liked this
markuso liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->