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

- 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/>{<br/><br/>var x 1;<br/><br/>}<br/><br/></script><br/></head><br/><body><br/><br/>Hello <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/>Displaying the browsers name and version number<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/><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/><br/>Its version is: 4.0 (compatible; MSIE 6.0b; Windows NT 5.0)<br/><br/>Changing the text in the status bar of the browser<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/>Using an input box to get text from the user<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/><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/>Using a message box to display text to the user<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/><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/>Changing the title of the browser window<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/>In Closing<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, because in some cases it can annoy your<br/>visitors and send them packing before your site even loads!<br/><p> About the Author <p>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</div><div class="panel panel-default" style="padding: 10px 10px 10px 10px;"><a class="btn btn-outline-primary btn-sm" href="/on/development/">development</a> <a class="btn btn-outline-primary btn-sm" href="/on/browser/">browser</a> <a class="btn btn-outline-primary btn-sm" href="/on/javascript/">javascript</a> <a class="btn btn-outline-primary btn-sm" href="/on/name/">name</a> <a class="btn btn-outline-primary btn-sm" href="/on/display/">display</a> <a class="btn btn-outline-primary btn-sm" href="/on/version/">version</a> </div> </div> </div> <!-- /#page-content-wrapper --> </div> <!-- /#wrapper --> <footer class="footer"> <div class="container-fluid text-right"> <p class="text-muted"><i class="fa fa-copyright" aria-hidden="true"></i> <a href="http://ondapc.com/" target="_blank">ondapc</a> project 2024</p> </div> </footer> <!-- CONTACT US --> <div class="modal" id="contact"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h4 class="modal-title"><i class="far fa-envelope"></i> Contact Me</h4> <button type="button" class="close" data-dismiss="modal"><i class="fa fa-times"></i></button> </div> <div class="modal-body"> <form id="contactar" name="contactar" method="post"> <input type="hidden" name="token" value="0f83b1a020207f39b769ed9fd1745124"> <input type="hidden" name="captcha" value=""> <div class="form-group"> <div class="input-group-prepend"> <span class="input-group-text"><i class="fas fa-user"></i></span> <input class="form-control" id="name" name="name" placeholder="Your Name" required="required" value="" type="text"> </div> </div> <div class="form-group"> <div class="input-group-prepend"> <span class="input-group-text"><i class="far fa-envelope"></i></span> <input class="form-control" id="email" name="email" placeholder="Email" required="required" value="" type="email"> </div> </div> <div class="form-group"> <div class="input-group-prepend"> <span class="input-group-text"><i class="fas fa-edit"></i></span> <textarea name="message" id="message" class="form-control" rows="9" cols="25" placeholder="Write a message" required="required"></textarea> </div> </div> </div> <div class="modal-footer"> <button type="submit" class="btn btn-primary">Send <i class="fas fa-chevron-right"></i></button> </div> </div> </div> </div> <div id="contact" class="modal fade" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><i class="fa fa-times"></i></button> <h4 class="modal-title"><i class="fa fa-envelope-o"></i> Contact Me</h4> </div> <div class="modal-body"> </div> </div> </div> <script> $(function(){ $('#contactar').on('submit', function(e){ e.preventDefault(); $.ajax({ url: '/contactx/', type: 'POST', data: $('#contactar').serialize(), success: function(data){ $('#contactar')[0].reset(); $("#contact-success").append('<span><i class="fa fa-check"></i> Message Sent!</span>'); } }); }); }); </script> <!-- Bootstrap core JavaScript --> <script src="vendor/jquery/jquery.min.js"></script> <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script> <!-- Menu Toggle Script --> <script> $("#menu-toggle").click(function(e) { e.preventDefault(); $("#wrapper").toggleClass("toggled"); }); </script> </body> </html> <!-- CACHE d19d52de8314e7cb2059151970deed4a 2024-04-27 10:34:48 -->