Examples of JavaScript Capabilities

Examples of JavaScript Capabilities thumbnail
JavaScript is one of the programming languages prominent on the Web

JavaScript serves as one of the fundamental languages of the Internet. It can parse and print HTML to Web browsers in order to display Web pages. It can perform complex mathematical computations like most other programming languages. JavaScript can also take information about the user or the user's Web browser and use it to make execution decisions. JavaScript also works on data from HTML forms and adds functionality to HTML inputs such as buttons.

  1. Printing HTML to the Browser

    • One of the basic capabilities of JavaScript is to write text to the Web browser. The Web browser, in turn, parses the text for display to the user. Because of this, programmers can use JavaScript to construct Web pages:

      document.write("<p>This is a paragraph</p><br />");

      document.write("<h1>This is a header</h1>");

      Because the string that the "document.write()" function writes to the browser contains HTML, the Web browser will parse the HTML and present the text as a regular HTML Web page. This allows the programmer to pass data to the user through a designed Web page.

    JavaScript and Math

    • JavaScript contains the functionality to perform simple and complex mathematical computations. The complexity of these computations can range from simple arithmetic to typical mathematical calculations:

      document.write(5+5);

      10

      document.write(Math.round(8.5));

      9

      JavaScript also contains variables to represent familiar mathematical constants such as pi. Because of this, programmers can use JavaScript to calculate most any value and return in to the user.

    Forms and Event

    • JavaScript can also add functionality to Web pages by defining actions to take when users interact with the Web page. For example, a Web page might contain a user input button. The programmer might define the button in the HTML code like this example:

      <form name="input">

      <input type="button" onClick="function1()">Click Me</input>

      </form>

      The "function1()" function will execute when the user clicks the button. In the header, then, there could exist a JavaScript "function1" that will execute a command:

      <head>

      <script type="text/javascript>

      function function1()

      {

      document.write("HI!");

      }

      </head>

      When the user clicks the button, JavaScript will write "HI!" to the browser.

    Get Data About the User

    • JavaScript also contains useful tools to gather data about a user and the user's browsing environment. JavaScript can use the navigator object to gather information about the name and version of the Web browser in use. The navigator object can also determine whether or not cookies, which are small files that store information about the user, are enabled:

      document.write(navigator.appName);

      document.write(navigator.appVersion);

      document.write(navigator.cookieEnabled);

      This way, the programmer can require certain browser types or versions and can limit page functionality if these requirements are not met.

Related Searches:

References

  • Photo Credit Hemera Technologies/PhotoObjects.net/Getty Images

Comments

Related Ads

Featured