Javascript Basics 01

Written by Lisa Spurlin

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

Start atrepparttar 105598 Beginning

Browsers know to interpret Web pages as HTML because ofrepparttar 105599 tags. Since JavaScript is contained inside an HTML document, it needs to be set apart withrepparttar 105600 tags.

TITLE< itle><p><script language="JavaScript"><!-- Comment from older browsers<p>YOUR SCRIPT HERE<p>// end commenting out --> </script> </head> <body><p>Don't forget that last </script> tag! Abrowser will try and interpret<IMG height=12 src="/the2.jpg" alt="repparttar 105601"> whole HTML page as JavaScript, until it comes to that closing tag. Without it,<IMG height=12 src="/the2.jpg" alt="repparttar 105602"> page will generate unsightly errors and not load properly.<p>Comment, Comment, Comment<p>Commenting code allows you, or someone else looking at it, to understand what's occuring in<IMG height=12 src="/the2.jpg" alt="repparttar 105603"> code. Commenting can be done in both single and multi-line variations:<p>// single line comments<p>/* multi-line comments */<p>But what about<IMG height=12 src="/the2.jpg" alt="repparttar 105604"> HTML comment <!-- --> inside<IMG height=12 src="/the2.jpg" alt="repparttar 105605"> script tags. That exists so older browsers that don't understand JavaScript won't try and interpret it. Otherwise,<IMG height=12 src="/the2.jpg" alt="repparttar 105606"> code will render<IMG height=12 src="/the2.jpg" alt="repparttar 105607"> page as HTML, resulting in<IMG height=12 src="/the2.jpg" alt="repparttar 105608"> page displaying incorrectly.<p>Defining Variables<p>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:<p>Numeric: var x = 0; String: var y = "hello"; Variables: var z = x + y; Object: var myImage = new Image(); <p>Strings MUST contain "" around<IMG height=12 src="/the2.jpg" alt="repparttar 105609"> word or phrase. Otherwise<IMG height=12 src="/the2.jpg" alt="repparttar 105610"> 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.<p>Ex: var x = hello ** wrong<p>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. <p>Ex: var y = "1"; var z = "2"; var a = y + z;<p>The variable "a" in this instance is "12" not 3, since<IMG height=12 src="/the2.jpg" alt="repparttar 105611"> two strings were combined together as text, not added like numbers. This would be true even if y = 1.<p>Making a Statement<p>Notice<IMG height=12 src="/the2.jpg" alt="repparttar 105612"> semi-colons (;) at<IMG height=12 src="/the2.jpg" alt="repparttar 105613"> end of each line of code? The semi-colon denotes<IMG height=12 src="/the2.jpg" alt="repparttar 105614"> end of that particular statement. While JavaScript can sometimes be forgiving if you don't include<IMG height=12 src="/the2.jpg" alt="repparttar 105615"> semi-colon at<IMG height=12 src="/the2.jpg" alt="repparttar 105616"> 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.<p>Alert! Alert!<p>Alerts are one of<IMG height=12 src="/the2.jpg" alt="repparttar 105617"> 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. <p>Examples of alerts<p>alert("this is a string"); creates an alert that will contain<IMG height=12 src="/the2.jpg" alt="repparttar 105618"> text"this is a string" <br><br></font></td><!-- google_ad_section_end --><!-- google_ad_section_start(weight=ignore) --><td align="top" width="10%"></td><td align="top" width="45%"><h2>Instant plug-in scripts that help you profit... "Special offer expiry date with a twist"</h2><font size="2">Written by Michel Komarov</font><br><br><script type="text/javascript"><!-- google_ad_client = "pub-5766870852072819"; google_ad_width = 234; google_ad_height = 60; google_ad_format = "234x60_as"; google_ad_channel ="9238851329"; google_color_border = "CFB9A1"; google_color_bg = "CFB9A1"; google_color_link = "000000"; google_color_url = "431B02"; google_color_text = "431B02"; //--></script> <script type="text/javascript" src=""> </script> <br> <font size="2">How would you like to add an irresistible sense of urgency to any of your online offers... instantly?<p>I thought so!<p>What I've go for you here is<IMG height=12 src="/the2.jpg" alt="repparttar "> perfect answer in<IMG height=12 src="/the2.jpg" alt="repparttar 105596"> form of a special piece of JavaScript you can just copy and paste into your sa'les page.<p>So what does it do exactly?<p>It creates an active deadline date counting down<IMG height=12 src="/the2.jpg" alt="repparttar 105597"> days until your offer expires.<p>So where's<IMG height=12 src="/the2.jpg" alt="repparttar 105598"> twist?<p>What's extra special about this unique code is as soon as<IMG height=12 src="/the2.jpg" alt="repparttar 105599"> offer is over<IMG height=12 src="/the2.jpg" alt="repparttar 105600"> HTML code you placed inside this script will no longer be seen on your website.<p>For example inside<IMG height=12 src="/the2.jpg" alt="repparttar 105601"> HTML code could be an order button giving your visitors<IMG height=12 src="/the2.jpg" alt="repparttar 105602"> chance to order at a discount.<p>As soon as<IMG height=12 src="/the2.jpg" alt="repparttar 105603"> special offer date has been reached<IMG height=12 src="/the2.jpg" alt="repparttar 105604"> discount order button just disappears!<p>I think this is much more honest way of working than just adding an advance special offer expiry date that's never reached.<p>Here's how it all works...<p>Place<IMG height=12 src="/the2.jpg" alt="repparttar 105605"> HTML code into<IMG height=12 src="/the2.jpg" alt="repparttar 105606"> source code of your web page anywhere between<IMG height=12 src="/the2.jpg" alt="repparttar 105607"> <body> and </body> tags. The position you choose is where your special offer will appear:<p><script language="JavaScript"><!-- // Script created by // Subscribe to for this // and many other essential web site scripts.<p> var expiry = new Date(2004,0,1);<p> // (2004,0,1) = (year, month, day) // months: 0 = Jan, 1 = Feb, 2 = Mar, 3 = Apr, // 4 = May, 5 = Jun, 6 = Jul, 7 = Aug, 8 = Sep, // 9 = Oct, 10 = Nov, 11 = Dec<p> var today = new Date(); var expiryDays = parseInt((expiry-today)/86400000); var expiryDate = expiry.toDateString(); if (expiry > today) document.write( <br><br></font></td><!-- google_ad_section_end --></tr><tr><td>Cont'd on page 2 ==<a class="mlink" href="2-Javascript_Basics_01-5597.htm">></a></td></tr></table><script type="text/javascript"><!-- google_ad_client = "pub-5766870852072819"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_channel ="8831454965"; google_color_border = "CFB9A1"; google_color_bg = "CFB9A1"; google_color_link = "000000"; google_color_url = "431B02"; google_color_text = "431B02"; //--></script> <script type="text/javascript" src=""> </script> </td> </tr> </table> <table width="770" border="0" cellspacing="0" cellpadding="0"> <tr> <td> </td> </tr> <tr> <td height="48" align="center" background="images/bg_nav_bottm.jpg"><span class="style3"> © 2005<br> <a href="terms.html" rel="nofollow">Terms of Use</a></span></td> </tr> </table></td> </tr> </table> <script type="text/javascript"> var HASH_ESCAPED="%23"; function TrackIt(adUnit){ if (window.status) { var adDomain = escape(window.status.substring(6)); var pyPage = document.location.pathname; var params =; var hasAnchor = params.lastIndexOf(HASH_ESCAPED)!= -1; params = hasAnchor? (params.substring(0, params.lastIndexOf(HASH_ESCAPED))) : params; pyPage = escape(pyPage.substring(pyPage.lastIndexOf('/') + 1)); pyPage = pyPage + params; var curTime = new Date().valueOf(); var bug = new Image(); bug.src = '/track/adsenseTrack.php?pyPage=' + pyPage + '&adDomain=' + adDomain + '&adUnit=' + adUnit + "&time=" + curTime; } } function TrackIt0() {TrackIt(0); } function TrackIt1() {TrackIt(1); } function TrackIt2() {TrackIt(2); } var elements = document.getElementsByTagName("iframe"); for (var i = 0; i < elements.length; i++) { if(elements[i].src.indexOf('') > -1) { //elements[i].onfocus = TrackIt; if (i==0) elements[i].onfocus = TrackIt0; if (i==1) elements[i].onfocus = TrackIt1; if (i==2) elements[i].onfocus = TrackIt2; } } </script> <!--WEBBOT bot="HTMLMarkup" startspan ALT="Site Meter" --> <script type="text/javascript" language="JavaScript">var site="s19improve"</script> <script type="text/javascript" language="JavaScript1.2" src=""> </script> <noscript> <a href="" target="_top"> <img src="" alt="Site Meter" border=0></a> </noscript> <!-- Copyright (c)2002 Site Meter --> <!--WEBBOT bot="HTMLMarkup" Endspan --> </body> </html>