Free Articles, Free Web Content, Reprint Articles
Saturday, February 11, 2012
 
Free Articles, Free Web Content, Reprint ArticlesRegisterAll CategoriesTop AuthorsSubmit Article (Article Submission)ContactSubscribe Free Articles, Free Web Content, Reprint Articles
ADVERTISEMENTS
 

Some Useful JavaScript Tricks

... can be one of the most useful ... to any webpage. It comes bundled with ... Internet Explorer ... ... and it allows us to perform field ... ima

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="http://www.articlesfactory.com"><img src="http://www.articlesfactory.com/pic/x.gif" alt="Article Search" border="0"></a>, because in some cases it can annoy your<br>visitors and send them packing before your site even loads! <!-- 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> <g:plusone size="medium" count="false"></g:plusone> <iframe src="//www.facebook.com/plugins/like.php?href&send=false&layout=button_count&width=450&show_faces=true&action=like&colorscheme=light&font&height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe> <p> <h1>ABOUT THE AUTHOR</h1> <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> <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/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/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/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/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/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/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/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/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/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/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/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/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/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/internet.html" class="left-menu-inactive">Internet</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/law.html" class="left-menu-inactive">Law</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://www.diyawards.com" target="_blank" rel="nofollow">Crystal Awards and Trophies</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://janeshilton.co.uk/handbags" target="_blank" rel="nofollow">Handbags</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 type="text/javascript"><!-- google_ad_client = "ca-pub-0003120597825310"; /* [AF] (120 x 600) */ google_ad_slot = "2900666160"; google_ad_width = 120; google_ad_height = 600; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </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.088 seconds</div>