Você está na página 1de 4

JavaScript Introduction

The JavaScript Introduction tutorial explains what JavaScript is and how to use it for web development.
Majid Yaseen

JavaScript works on the Client Side (on the user's computer).

The primary purpose of JavaScript is to provide a better experience for the user. It manipulates the objects within the HTML document. The objects and HTML elements collectively form what we call theDocument Object Model (DOM). We will get into XHTML and XML later, but for now, we are going to use JavaScript with the HTML document and Cascading Styles Sheets (CSS). A little bit of history on JavaScript. Netscape Communications Corporation invented JavaScript back in 1995. Many different languages (from C to Python) influenced the syntax and structure of JavaScript. JavaScript is arguably one of the most flexible languages ever created. However, JavaScript is one of those languages that people claim to know, but have absolutely no idea how complex JavaScript can get. We won't dive super far into JavaScript, but we will build a fundamental base for you.

What exactly does JavaScript do?


It allows programming to be performed without server interaction. It can respond to events, such as button clicks. It can validate data before sending out a request. It can adjust an HTML document for special effects It can create cookies! Cookies can be used to store and retrieve information from the user's computer.

JavaScript is written into an HTML document in 2 ways:


Using: <script type="text/JavaScript"> Where you would embed your code here </script> Or referencing it kind of like an external CSS: <script type="text/JavaScript" src="myJavaScript.js"></script>

Below is a very simple HTML Skeleton with our JavaScript embedded using the first method. Example 1 <html> 2 <body> 3 <h3>My First JavaScript Code</h3> 4 <script type="text/JavaScript"> 5 document.write("<p>Hello!</p>"); 6 </script> 7 </body> 8</html> Below is how the above code would look in a browser. Result

My First JavaScript Code


Hello! First, let's break down document.write("<p>Hello!</p>") JavaScript is a Object Based Programming Language. So, in the example above document is the object. In JavaScript, an object followed by a . , such as document. , is an indication that a method or an property will follow. (Also, some programmers often refer to properties as attributes.) The write here is actually one of the document's methods. Methods generally have arguments, and in this case, the <p>Hello!</p> is the argument. Now let's understand an object's property. Example 1<html> 2 <body> 3 <h3>JavaScript Object Properties</h3>

<p> 4 <script type="text/JavaScript"> 5 document.write(document.location); 6 </script> 7 </p> 8 </body> 9 </html> 10

Result

JavaScript Object Properties


http://www.afterhoursprogramming.com/tutorial/JavaScript/Introduction/ In this example, you can see that we use the document's property, document.location, as an argument for the method, document.write. The document.location is equal tohttp://www.afterhoursprogramming.com/tutorial/JavaScript/Introduction/, but since it was in the method document.write, it actually wrote that to the DOM so that we could see it.

Você também pode gostar