Free Articles, Free Web Content, Reprint Articles
Thursday, November 22, 2018
 
Free Articles, Free Web Content, Reprint ArticlesRegisterAll CategoriesTop AuthorsSubmit Article (Article Submission)ContactSubscribe Free Articles, Free Web Content, Reprint Articles
 

Javascript Basics 01

JavaScript adds simple or sophisticated interactivity to a Web site, enhancing the user's experience. Like any programming language, you need to understand the building blocks before you can start pro...

JavaScript adds simple or sophisticated interactivity to a Web site, enhancing the user's experience. Like any programming language, you need to understand the building blocks before you can start programming.

Start at the Beginning

Browsers know to interpret Web pages as HTML because of the tags. Since JavaScript is contained inside an HTML document, it needs to be set apart with the tags.



TITLE< itle><br><br><script language="JavaScript"><!-- Comment from older browsers<br><br>YOUR SCRIPT HERE<br><br>// end commenting out --><br></script><br></head><br><body><br><br>Don't forget that last </script> tag! Abrowser will try and interpret the whole HTML page as JavaScript, until it comes to that closing tag. Without it, the page will generate unsightly errors and not load properly.<br><br>Comment, Comment, Comment<br><br>Commenting code allows you, or someone else looking at it, to understand what's occuring in the code. Commenting can be done in both single and multi-line variations:<br><br>// single line comments<br><br>/* multi-line<br>comments */<br><br>But what about the HTML comment <!-- --> inside the script tags. That exists so older browsers that don't understand JavaScript won't try and interpret it. Otherwise, the code will render the page as HTML, resulting in the page displaying incorrectly.<br><br>Defining Variables<br><br>JavaScript, like all programming languages, uses variables to store information. These variables can store numbers, strings, variables that have been previously defined, and objects. For example:<br><br>Numeric: var x = 0; <br>String: var y = "hello"; <br>Variables: var z = x + y; <br>Object: var myImage = new Image(); <br><br>Strings MUST contain "" around the word or phrase. Otherwise the JavaScript will interpret it as a number. Numbers and previously defined variables, likewise, should not have "" unless you want that number to be treated as a string.<br><br>Ex: var x = hello ** wrong<br><br>Variables that store numbers and strings can be combined in a new variable. However, if anything is combined with a string, it is automatically be treated as a string. <br><br>Ex: var y = "1";<br>var z = "2";<br>var a = y + z;<br><br>The variable "a" in this instance is "12" not 3, since the two strings were combined together as text, not added like numbers. This would be true even if y = 1.<br><br>Making a Statement<br><br>Notice the semi-colons (;) at the end of each line of code? The semi-colon denotes the end of that particular statement. While JavaScript can sometimes be forgiving if you don't include the semi-colon at the end of each statement, it's good practice to remember to do so. Otherwise, you might not remember to put it there when you really need it.<br><br>Alert! Alert!<br><br>Alerts are one of the greatest functions in JavaScript. They not only pass information on to visitors, but help you when you're trying to hunt down a bug in your code. <br><br>Examples of alerts<br><br>alert("this is a string"); <br>creates an alert that will contain the text"this is a string" <br><br>alert(x) <br>creates an alert that will contain the value defined in variable x (make sure that variable x is defined before calling it)<br><br>alert("the number is " + x); <br>-creates an alert that will combine text and the value in x.<br><br>It Can Do Math Too?<br><br>JavaScript wouldn't be much of a programming language if it couldn't do simple math. While there are dozens of complex, built-in math functions, here are the basic symbols you'll need to know:<br><br>addition + <br>subtraction - <br>multiplication * <br>division / <br>greater than > <br>less than < <br>greater than or equal >= <br>less than or equal <= <br><br>What "If" Statements<br><br>"If" statements are often used to compare values. If the statement is true, a set of instructions enclosed in {} executes. Comparisons like this are done using the following symbols:<br><br>equals == <br>not equal != <br><br>In the example below, you can see how an "if" statement is used to determine text that displays in an alert window. Copy and paste the following script into an HTML page to see it at work.<br><br><script language="JavaScript"><br><!-commenting from old browsers<br>var x = 6;<br>var y = 3;<br><br>// if statement for first alert<br>if ( x == 6 )<br>{<br>alert("x is equal to 6");<br>}<br>else<br>{<br>alert("x is not equal to 6");<br>}<br><br>// defining variable for second alert<br>var z = x + y;<br>alert(z);<br>// end commenting-- ></script><br><br>Notice how the instructions for each statement (both the "if" and "else") are enclosed in {}(curly brackets). All curly brackets must have a beginning and ending, just like HTML tags. If a curly bracket is missing the JavaScript will return an error, so be sure to proof your code. <br><br>________________________________________<br>This document is provided for informational purposes only, and i-Netovation.com makes no warranties, either expressed or implied, in this document. Information in this document is subject to change without notice. The entire risk of the use or the results of the use of this document remains with the user. The example companies, organizations, products, people, and events depicted herein are fictitious. No association with any real company, organization, product, person, or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, wit hout the express written permission of i-Netovation.com.<br><br>If you believe that any of these documents, related materials or any other i-Netovation.com content materials are being reproduced or transmitted without permission<a href="http://www.articlesfactory.com/articles/psychology.html"><img src="http://www.articlesfactory.com/pic/x.gif" alt="Psychology Articles" border="0"></a>, please contact: violation@i-netovation.com.<br><br>The names of actual companies and products mentioned herein may be the trademarks of their respective owners. <!-- google_ad_section_end --> <p class="txt-small-regular">Source: <a href="http://www.articlesfactory.com" class="small-link" title="Free Articles">Free Articles</a> from ArticlesFactory.com</p> <table> <tr> <td valign="top"><g:plusone size="medium" count="false"></g:plusone></td> <td valign="top"><div class="fb-like" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false"></div></td> <tr> </table> <p> <h1>ABOUT THE AUTHOR</h1> <br> <div>(C) Copyright 2002. Lisa Spurlin<br></div> </p> <center> <!-- ValueClick Media 300x250 Medium Rectangle CODE for articlesfactory.com --> <script language="javascript" src="http://media.fastclick.net/w/get.media?sid=20894&m=6&tp=8&d=j&t=n"></script> <noscript><a href="http://media.fastclick.net/w/click.here?sid=20894&m=6&c=1" target="_blank"> <img src="http://media.fastclick.net/w/get.media?sid=20894&m=6&tp=8&d=s&c=1" width=300 height=250 border=1></a></noscript> <!-- ValueClick Media 300x250 Medium Rectangle CODE for articlesfactory.com --> </center> <!-- Kontera ContentLink --> <SCRIPT LANGUAGE="JavaScript"> var dc_UnitID = 14; var dc_PublisherID = 3523; var dc_AdLinkColor = '#990000'; var dc_underlineType = 'solid'; var dc_open_new_win = 'yes'; var dc_adprod='ADL'; </SCRIPT> <SCRIPT LANGUAGE="JavaScript" SRC="http://kona.kontera.com/javascript/lib/KonaLibInline.js"></SCRIPT> <!-- Kontera ContentLink --> <br> <br> </div> </td> </tr> <tr> <td width="131" height="24" bgcolor="#EDE9E3" valign="top"> <table width="131" height="24" border="0" cellspacing="0" cellpadding="0" align="center"> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/health.html" class="left-menu-inactive">Health</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/business.html" class="left-menu-inactive">Business</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/finance.html" class="left-menu-inactive">Finance</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/travel.html" class="left-menu-inactive">Travel</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/technology.html" class="left-menu-inactive">Technology</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/home-repair.html" class="left-menu-inactive">Home Repair</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/computers.html" class="left-menu-inactive">Computers</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/marketing.html" class="left-menu-inactive">Marketing</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/autos.html" class="left-menu-inactive">Autos</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/family.html" class="left-menu-inactive">Family</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/entertainment.html" class="left-menu-inactive">Entertainment</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/law.html" class="left-menu-inactive">Law</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/education.html" class="left-menu-inactive">Education</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/communication.html" class="left-menu-inactive">Communication</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/other.html" class="left-menu-inactive">Other</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/sports.html" class="left-menu-inactive">Sports</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/ecommerce.html" class="left-menu-inactive">ECommerce</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/home-business.html" class="left-menu-inactive">Home Business</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/self-help.html" class="left-menu-inactive">Self Help</a></td> </tr> <tr> <td height="24" background="http://www.articlesfactory.com/pic/left_menu_inactive.gif"><a href="http://www.articlesfactory.com/articles/internet.html" class="left-menu-inactive">Internet</a></td> </tr> </table> <table width="131" height="11" border="0" cellspacing="0" cellpadding="0" bgcolor="#EDE9E3"> <tr> <td height="11" align="right"><img src="http://www.articlesfactory.com/pic/right_curve.gif" width="12" height="11" alt="" border="0"></td> </tr> </table> <!-- google_ad_section_start(weight=ignore) --> <div class="left-section"><b>Partners</b></div> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td><img src="http://www.articlesfactory.com/pic/x.gif" width="15" height="1" alt="" border="0"></td> <td valign="top"><img src="http://www.articlesfactory.com/pic/left_menu_bullet.gif" width="8" height="10" alt="" border="0"></td> <td><div class="left-section-link"><a class="left-section-link" href="http://www.bailfish.com" target="_blank" rel="nofollow">California Bail Bonds Bounty Hunters</a></div></td> </tr> <tr> <td><img src="http://www.articlesfactory.com/pic/x.gif" width="15" height="1" alt="" border="0"></td> <td valign="top"><img src="http://www.articlesfactory.com/pic/left_menu_bullet.gif" width="8" height="10" alt="" border="0"></td> <td><div class="left-section-link"><a class="left-section-link" href="http://www.kateraynor.com" target="_blank" rel="nofollow">Immigration Attorney</a></div></td> </tr> <tr> <td><img src="http://www.articlesfactory.com/pic/x.gif" width="15" height="1" alt="" border="0"></td> <td valign="top"><img src="http://www.articlesfactory.com/pic/left_menu_bullet.gif" width="8" height="10" alt="" border="0"></td> <td><div class="left-section-link"><a class="left-section-link" href="http://www.movingcompanies.tv" target="_blank" rel="nofollow">Moving Relocation Services</a></div></td> </tr> <tr> <td><img src="http://www.articlesfactory.com/pic/x.gif" width="15" height="1" alt="" border="0"></td> <td valign="top"><img src="http://www.articlesfactory.com/pic/left_menu_bullet.gif" width="8" height="10" alt="" border="0"></td> <td><div class="left-section-link"><a class="left-section-link" href="http://caraudiosecurity.com/shop/home.html" target="_blank" rel="nofollow">Alpine Car Audio</a></div></td> </tr> <tr> <td><img src="http://www.articlesfactory.com/pic/x.gif" width="15" height="1" alt="" border="0"></td> <td valign="top"><img src="http://www.articlesfactory.com/pic/left_menu_bullet.gif" width="8" height="10" alt="" border="0"></td> <td><div class="left-section-link"><a class="left-section-link" href="http://www.citrusfresh.com" target="_blank" rel="nofollow">Carpet Cleaning Los Angeles</a></div></td> </tr> </table> <div align="center"> <script id="mNCC" language="javascript"> medianet_width='120'; medianet_height= '600'; medianet_crid='627327646'; </script> <script id="mNSC" src="http://contextual.media.net/nmedianet.js?cid=8CUIRZYTL" language="javascript"></script> </div> <br> <br> <!-- google_ad_section_end(weight=ignore) --> </td> </tr> </table> <table width="750" border="0" cellspacing="0" cellpadding="0" align="center"> <tr> <td height="50"> <div class="small-link" align="center"> <a class="small-link" href="http://www.articlesfactory.com/">Free Articles Home</a> | <a class="small-link" href="http://www.articlesfactory.com/signup.html">Register</a> | <a class="small-link" href="http://www.articlesfactory.com/submit.html">Submit an Article</a> | <a class="small-link" href="http://www.articlesfactory.com/links.html">Links</a> | <a class="small-link" href="http://www.articlesfactory.com/contact.html">Contact</a> | <a class="small-link" href="http://www.articlesfactory.com/archive.html">Archive</a> | <a class="small-link" href="http://www.articlesfactory.com/rss.html"><img src="http://www.articlesfactory.com/pic/icon-xml-rss_red.gif" width="52" height="13" alt="XML/RSS Feed" border="0" align="middle"></a> </div> <div class="small-link" align="center"> <a class="small-link" href="http://www.articlesfactory.com/terms.html">Terms of Service</a> | <a class="small-link" href="http://www.articlesfactory.com/privacy.html">Privacy Policy</a> </div> </td> </tr> </table> <!-- Start of StatCounter Code --> <script type="text/javascript" language="javascript"> <!-- var sc_project=479039; var sc_invisible=1; var sc_partition=3; var sc_security=""; //--> </script> <script type="text/javascript" language="javascript" src="http://www.statcounter.com/counter/counter.js"></script><noscript><img src="http://c4.statcounter.com/counter.php?sc_project=479039&java=0&security=&invisible=1" alt="free web stats" border="0"></noscript> <!-- End of StatCounter Code --> <!-- Start of Google Analytics Code --> <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-88528-1"; urchinTracker(); </script> <!-- End of Google Analytics Code --> <script> //window.resizeTo(1024, 768); //window.resizeTo(800, 600); </script> </body> </html><div class="small" align="center">Page loaded in 0.367 seconds</div>