Tuesday, April 10, 2012

Understanding Javascript

JavaScript is the most popular scripting language on the Internet due to it's wide acceptance with all major browsers. If you stumble across this blog and you don't have an understanding of HTML and CSS then my suggestion is to start learning those two languages first and then dabble with JavaScript. Either way, let's dive into JavaScript and cover some basics on its capabilities and how it is coded.

What can JavaScript do for your web pages? Well first and foremost JavaScript was designed for the purpose of adding interactivity to web pages. JavaScript can be embedded right into HTML pages. Specifically, a few examples of what JavaScript can do is react to events, validate data such as forms (like a form you fill out on a website asking for certain information), and create cookies. One important thing to remember about the difference between HTML and JavaScript is that JavaScript is case sensitive whereas HTML is not.

To insert JavaScript into an HTML file you use the <script> tag. Advancing on this you will typically write:

<script type="text/javascript">
....some javascript code....
</script>

Whatever is contained between the opening <script> and closing </script> tags is what is executed by the browser when the page is loaded. But what if we don't want JavaScript to execute right away, rather we want it to execute when an event occurs. An event could be when a user clicks a button. If this is the case then we need to add a function inside of our <script> tags:

<script type="text/javascript">
function displayDate ()

It is common to put any JavaScript that involves functions within the <head> tags of an HTML document. This makes for better organization and also ensures that the JavaScript will not interfere with the page content between the opening and closing <body> tags.

Next I just want to quickly mention JavaScript statements and what is referred to as Javascript Code (or simply JavaScript). A JavaScript statement is kind of what it sounds like. It is a command to a browser. For example, the following line of code would tell the browser to write "Hi I'm Jordan" onto the web page:

document. write("Hi I'm Jordan");

Javascript Code is a sequence of multiple JavaScript statements. For example:

<script type="text/javascript">
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph</p>");
document.write("<p>This is another paragraph</p>");
</script>

This is merely scratching the surface of JavaScript and how important is to web design and development. I simply wanted to introduce you into what JavaScript is and show you a few examples of what the Javascript language looks like. I'm definitely at the beginner spectrum of JavaScript myself so I feel your pain if you are thinking JavaScript is "another programming language I have to learn ON TOP of HTML and CSS". There are tons of resources out there to help you begin your quest with JavaScript. One site I recently registered for was www.codeacademy.com. Let me know what you think, I would love to hear some of your comments with regards to JavaScript or my blog in general!

No comments:

Post a Comment