Free Technology Articles
Wednesday, August 20, 2008
 
JAVASCRIPT ARTICLES

Some Useful JavaScript Tricks

JavaScript can be one of the most useful additions to any web page. It comes bundled with Microsoft Internet Explorer and Netscape Navigator and it allows us to perform field validations, mouse-overs images, open popup windows, and a slew of other things. In this article I will show you how to: - Display the browser name and version number - Change the text in the status bar of the browser - Use an input box to get text from the user - Use a message box to display text to the user - C...

JavaScript can be one of the most useful additions to any web
page. It comes bundled with Microsoft Internet Explorer and
Netscape Navigator and it allows us to perform field validations,
mouse-overs images, open popup windows, and a slew of other
things.

In this article I will show you how to:

- Display the browser name and version number
- Change the text in the status bar of the browser
- Use an input box to get text from the user
- Use a message box to display text to the user
- Change the title of the browser window

Before that, however, we need to know how to setup our web page
so that it can run the JavaScript. JavaScript code is inserted
between opening and closing script tags: ,
like this:



These script tags can be placed anywhere on the page, however
it's common practice to place them between the and
tags. A basic HTML page that contains some JavaScript looks
like this:



My Test Page < itle><br><script language="JavaScript"><br><br> function testfunc()<br> {<br> var x = 1;<br> }<br><br></script><br></head><br><body><br> <h1>Hello</h1><br></body><br></html><br><br>For the examples in this article, you should use the basic<br>document format I have just shown you, inserting the JavaScript<br>code between the <script> and </script> tags. When you load the<br>page in your browser, the JavaScript code will be executed<br>automatically.<br><br>-----------------------------------------------<br>Displaying the browsers name and version number<br>-----------------------------------------------<br><br>The "navigator" object in JavaScript contains the details of the<br>users browser, including its name and version number. We can<br>display them in our browser using the document.write function:<br><br> document.write("Your browser is: " + navigator.appName);<br> document.write("<br>Its version is: " + navigator.appVersion);<br><br>I run Windows 2000 and Internet Explorer version 6, so the output<br>from the code above looks like this in my browser window:<br><br> Your browser is: Microsoft Internet Explorer<br> Its version is: 4.0 (compatible; MSIE 6.0b; Windows NT 5.0)<br><br>-----------------------------------------------<br>Changing the text in the status bar of the browser<br>-----------------------------------------------<br><br>To change the text in the status bar of a browser window, we just<br>change the "status" member of the "window" object, which<br>represents our entire browser window:<br><br> window.status = "This is some text";<br><br>-----------------------------------------------<br>Using an input box to get text from the user<br>-----------------------------------------------<br><br>Just like in traditional windows applications, we can use an<br>input box to get some text input from the user. The "prompt"<br>function is all we need:<br><br> var name = prompt("What is your name?");<br> document.write("Hello " + name);<br><br>The prompt function accepts just one argument (the title of the<br>input box), and returns the value entered into the text box. In<br>the example above, we get the users name and store it in the<br>"name" variable. We then use the "document.write" function to<br>output their name into the browser window.<br><br>-----------------------------------------------<br>Using a message box to display text to the user<br>-----------------------------------------------<br><br>We can display a message box containing an OK button. These<br>are great when you want to let the user know what is happening<br>during their time on a particular page. We can use a message box<br>to display the "name" variable from our previous example:<br><br> var name = prompt("What is your name?");<br> alert("Your name is: " + name);<br><br>The "alert" function takes one argument, which is the text to<br>display inside of the message box.<br><br>-----------------------------------------------<br>Changing the title of the browser window<br>-----------------------------------------------<br><br>To change the title of our web browser's window, we simply modify<br>the "document.title" variable, like this:<br><br> document.title = "My new title";<br><br>One bad thing about the "document.title" variable is that we can<br>only manipulate it in Microsoft Internet Explorer. Netscape's<br>implementation of JavaScript doesn't allow us to modify it.<br><br>-----------------------------------------------<br>In Closing<br>-----------------------------------------------<br><br>As you can see from the examples in this article, JavaScript is a<br>powerful scripting language that we can use to enhance our<br>visitors experience with our site. You shouldn't use JavaScript<br>too much, however<a href=""><img src="http://www.hitecharticles.com/pic/x.gif" alt="" border="0"></a>, because in some cases it can annoy your<br>visitors and send them packing before your site even loads! <p> <b>ABOUT THE AUTHOR</b> <br> <div>Mitchell Harper is the founder of http://www.devarticles.com.<br>DevArticles provides its visitors with useful, informative<br>articles on ASP, PHP, and .NET, as well as heaps of tips and<br>tricks that you wont find anywhere else! To see what it's all<br>about, visit devArticles right now at http://www.devarticles.com<br></div> </p> </td> </tr> </table> </td> <td valign="top"> <table width="222" height="31" cellpadding="0" cellspacing="0" border="0" background="http://www.hitecharticles.com/pic/title_bgr.gif"> <tr> <td><div class="title">TECHNOLOGY NEWSLETTER</div></td> </tr> </table> <table width="222" cellpadding="11" cellspacing="0" border="0" bgcolor="#f3f3f3"> <form action="" name="subscribe" method="post" onSubmit="return validateSubscribeForm(this)"> <tr> <td> <div class="aanot">Free Technology Articles provides real-time coverage of Technology news and articles.</div> <br> <div class="red"><b>Free Email Newsletters:</b></div> <br> <table width="100%" cellpadding="1" cellspacing="0" border="0"> <tr> <td><input type="checkbox" name="category_id[]" value="6"> <a class="alinks" href="http://www.hitecharticles.com/articles/cgi.html">CGI</a></td> <td><input type="checkbox" name="category_id[]" value="7"> <a class="alinks" href="http://www.hitecharticles.com/articles/computers.html">Computers</a></td> </tr> <tr> <td><input type="checkbox" name="category_id[]" value="9"> <a class="alinks" href="http://www.hitecharticles.com/articles/css.html">CSS</a></td> <td><input type="checkbox" name="category_id[]" value="10"> <a class="alinks" href="http://www.hitecharticles.com/articles/dhtml.html">DHTML</a></td> </tr> <tr> <td><input type="checkbox" name="category_id[]" value="12"> <a class="alinks" href="http://www.hitecharticles.com/articles/domain-names.html">Domain Names</a></td> <td><input type="checkbox" name="category_id[]" value="26"> <a class="alinks" href="http://www.hitecharticles.com/articles/html.html">HTML</a></td> </tr> <tr> <td><input type="checkbox" name="category_id[]" value="28"> <a class="alinks" href="http://www.hitecharticles.com/articles/internet.html">Internet</a></td> <td><input type="checkbox" name="category_id[]" value="29"> <a class="alinks" href="http://www.hitecharticles.com/articles/javascript.html">Javascript</a></td> </tr> <tr> <td><input type="checkbox" name="category_id[]" value="46"> <a class="alinks" href="http://www.hitecharticles.com/articles/science.html">Science</a></td> <td><input type="checkbox" name="category_id[]" value="56"> <a class="alinks" href="http://www.hitecharticles.com/articles/technology.html">Technology</a></td> </tr> </table> </td> </tr> <tr bgcolor="#e5e5e5"> <td> <script src="http://www.hitecharticles.com/js/newsletter.js" type="text/javascript"></script> <input type="hidden" name="cmd" value="subscribe"> <input type="text" name="email" value="Enter Your Email" onclick="this.value = '';" maxlength="100"> <input type="submit" name="submit" value="Subscribe"> </td> </tr> </form> </table> <table width="222" cellpadding="0" cellspacing="0" border="0" bgcolor="#f3f3f3"> <tr bgcolor="#ffffff"> <td height="2"><img src="http://www.hitecharticles.com/pic/x.gif" width="1" height="2" alt="" border="0"></td> </tr> </table> <table width="222" height="100" cellpadding="3" cellspacing="0" border="0" bgcolor="#d7d7d7"> <tr> <td align="center"><!-- ValueClick Media 120x600 and 160x600 SkyScraper CODE for hitecharticles.com --> <script language="javascript" src="http://media.fastclick.net/w/get.media?sid=26100&m=3&tp=7&d=j&t=n"></script> <noscript><a href="http://media.fastclick.net/w/click.here?sid=26100&m=3&c=1" target="_blank"> <img src="http://media.fastclick.net/w/get.media?sid=26100&m=3&tp=7&d=s&c=1" width=160 height=600 border=1></a></noscript> <!-- ValueClick Media 120x600 and 160x600 SkyScraper CODE for hitecharticles.com --></td> </tr> </table> <table width="222" cellpadding="0" cellspacing="0" border="0" bgcolor="#f3f3f3"> <tr bgcolor="#ffffff"> <td height="2"><img src="http://www.hitecharticles.com/pic/x.gif" width="1" height="2" alt="" border="0"></td> </tr> </table> <table width="222" height="31" cellpadding="0" cellspacing="0" border="0" background="http://www.hitecharticles.com/pic/title_bgr.gif"> <tr> <td><div class="title">CALENDAR</div></td> </tr> </table> <table width="222" cellpadding="11" cellspacing="0" border="0" bgcolor="#f3f3f3"> <tr> <td> <table align="center" border="0" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF" class="calendar"> <div align="center" class="calendar-month"><a class="calendar-month" href="/articles/month/2008-08.html">August 2008</a></div> <tr><th abbr="Sunday">S</th><th abbr="Monday">M</th><th abbr="Tuesday">T</th><th abbr="Wednesday">W</th><th abbr="Thursday">T</th><th abbr="Friday">F</th><th abbr="Saturday">S</th></tr> <tr><td colspan="5"> </td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-01.html">1</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-02.html">2</a></td></tr> <tr><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-03.html">3</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-04.html">4</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-05.html">5</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-06.html">6</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-07.html">7</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-08.html">8</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-09.html">9</a></td></tr> <tr><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-10.html">10</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-11.html">11</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-12.html">12</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-13.html">13</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-14.html">14</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-15.html">15</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-16.html">16</a></td></tr> <tr><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-17.html">17</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-18.html">18</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-19.html">19</a></td><td class="linked-day"><a class="calendar-day-active" href="/articles/day/2008-8-20.html">20</a></td><td class="calendar-day-inactive"><div class="calendar-day-inactive">21</div></td><td class="calendar-day-inactive"><div class="calendar-day-inactive">22</div></td><td class="calendar-day-inactive"><div class="calendar-day-inactive">23</div></td></tr> <tr><td class="calendar-day-inactive"><div class="calendar-day-inactive">24</div></td><td class="calendar-day-inactive"><div class="calendar-day-inactive">25</div></td><td class="calendar-day-inactive"><div class="calendar-day-inactive">26</div></td><td class="calendar-day-inactive"><div class="calendar-day-inactive">27</div></td><td class="calendar-day-inactive"><div class="calendar-day-inactive">28</div></td><td class="calendar-day-inactive"><div class="calendar-day-inactive">29</div></td><td class="calendar-day-inactive"><div class="calendar-day-inactive">30</div></td></tr> <tr><td class="calendar-day-inactive"><div class="calendar-day-inactive">31</div></td><td colspan="6"> </td></tr> </table> </td> </tr> </table> <table width="222" cellpadding="0" cellspacing="0" border="0" bgcolor="#f3f3f3"> <tr bgcolor="#ffffff"> <td height="2"><img src="http://www.hitecharticles.com/pic/x.gif" width="1" height="2" alt="" border="0"></td> </tr> </table> <table width="222" height="31" cellpadding="0" cellspacing="0" border="0" background="http://www.hitecharticles.com/pic/title_bgr.gif"> <tr> <td><div class="title">INTERNET RESOURCES</div></td> </tr> </table> <table width="222" cellpadding="11" cellspacing="0" border="0" bgcolor="#f3f3f3"> <tr> <td> <a class="alinks" href="http://www.itmask.com/services/web-design-pricing/" target="_blank">Web Design Pricing</a><br> <a class="alinks" href="http://www.financeperson.com/" target="_blank">Finance Articles</a><br> <a class="alinks" href="http://www.classical-literature.com/" target="_blank">Classical Literature</a><br> <a class="alinks" href="http://www.bailyes.com/maryland.htm" target="_blank">Maryland Bail Bonds Company</a><br> <a class="alinks" href="http://www.sapsoftwaretutorials.com/software/sap-logistics.html" target="_blank">SAP Logistics</a><br> </td> </tr> </table> <table width="222" cellpadding="0" cellspacing="0" border="0" bgcolor="#f3f3f3"> <tr bgcolor="#ffffff"> <td height="2"><img src="http://www.hitecharticles.com/pic/x.gif" width="1" height="2" alt="" border="0"></td> </tr> </table> <table width="222" cellpadding="0" cellspacing="0" border="0" bgcolor="#f3f3f3"> <tr bgcolor="#cfcfcf"> <td height="3"><img src="http://www.hitecharticles.com/pic/x.gif" width="1" height="3" alt="" border="0"></td> </tr> <tr bgcolor="#ffffff"> <td height="2"><img src="http://www.hitecharticles.com/pic/x.gif" width="1" height="2" alt="" border="0"></td> </tr> </table> </td> </tr> </table> </td> </tr> </table> <table width="994" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff" align="center"> <tr> <td height="2"><img src="http://www.hitecharticles.com/pic/x.gif" width="1" height="2" alt="" border="0"></td> </tr> </table> <table width="994" cellpadding="11" cellspacing="2" border="0" bgcolor="#f3f3f3" align="center"> <tr bgcolor="#cfcfcf"> <td> <a class="alinks" href="http://www.hitecharticles.com/">Home</a> | <a class="alinks" href="http://www.hitecharticles.com/top-authors.html">Top Authors</a> | <a class="alinks" href="http://www.hitecharticles.com/submit.html">Submit Article</a> | <a class="alinks" href="http://www.hitecharticles.com/archive.html">Archive</a> | <a class="alinks" href="http://www.hitecharticles.com/rss.html">RSS Feeds</a> </td> </tr> </table> <!-- Start of StatCounter Code --> <script type="text/javascript" language="javascript"> var sc_project=1325592; var sc_invisible=1; var sc_partition=9; var sc_security="11558c78"; </script> <script type="text/javascript" language="javascript" src="http://www.statcounter.com/counter/counter.js"></script><noscript><img src="http://c10.statcounter.com/counter.php?sc_project=1325592&java=0&security=11558c78&invisible=1" alt="website stat" border="0"></noscript> <!-- End of StatCounter Code --> <script> //window.resizeTo(1024, 768); //window.resizeTo(800, 600); </script> </body> </html><br><div class="small" align="center">Page loaded in 0.085 seconds</div>