JavaScript started its life in 1995. It was called LiveScript at the time and it was made for Netscape Navigator 2.0. After 2 months, Netscape and Sun Microsystems turned the name into JavaScript, which is what we call it today. This caused programming chaos at that time, because many people got confused about the difference between JavaScript and Java. Microsoft's response was Internet Explorer 3.0 with something called Jscript, to avoid copyright problems. Be aware though, Java is a web application, JavaScript is a client side scripting language. For more information on the history of JavaScript, Click HERE
JavaScript is a client side scripting language that is supposed to do tasks. HTML is made just for displaying content. It does not have the power to manipulate the content or do other things to the content. JavaScript is here to do that. Before, the stuff done by the client (or viewer) would be passed to a CGI script for processing, than returned back to the HTML page. It works, but it's clumsy and slow. Think of that as normal mail. JavaScript is basically e-mail. It works faster; since it is directly implanted on the webpage rather put somewhere 5000 miles away where the CGI script is being pounded by 1000's of requests all the time. It's like giving a note to every student in a school to see, rather than posting it up on a bulletin board and having all 600 students gather together to read and memorize the contents of a 8.5 by 11 piece of paper. So, JavaScript is simply designed to do stuff. That's all there is to it.
Putting JavaScript on a webpage is very simple. There are two main ways.
Both use a script tag.
1. Use a; <script type="text/javascript">//somecode</script>
2. Include external JavaScript files; <script src="somefilename.js"/>
For this tutorial, we'll stick to the first one to avoid relative and full url as well as hosting problems.
1.The 1'st way to do this is to put two slashes. The only problem is that this only applies to that line.
For Example:
1."return true;". It continues to run.
Which will result in this:I will go to google
This is it! What you have been waiting for! Your first JavaScript code!
For your first code, you are going to be using something called document.write.
Open a text editor, anything will work, even something as simple as notepad if you are using a Window's computer. Just one thing, if you are running Window's, do not, I repeat, do not use WordPad.
Well, here is the code!
Save the file as an .html file, and open it in your browser.
Okay, now let's analyze the code.
1. It includes the basic html stuff(body,head...).
2. It includes the <script> tag, with the type declared as text/javascript.
3. Okay, now the command document.write. It means what it looks like, document.write, or document write
Now, after that,
("Hello World!");.
This (""); is the container for everything, doing different things depending on the command, in this case, it writes what is in it.
You could even include html tags in there! For example, you could write,
and it will display as
I just wrote my first piece of JavaScript code!!
However do not rely on this to write scads of html code, because
THIS:
is simply inconventional, and anyway, you have to put everything on that line, or it won't work.