10 Stellar Web Resources for HTML5 and CSS3
How to Register a Domain Name for Your HTML5 and CSS3 Site
How to Use IrfanView’s Built-In Effects for HTML5 and CSS3 Programming

How to Use JavaScript’s Built-In Functions to Program with HTML

JavaScript gives you access a number of built-in functions, including the prompt() function, which lets you ask the user for written input. As with the confirm() function, you provide a text prompt to ask the user to provide a value. On return, you set the output of the function equal to the prompt() function and use the data in your application.

Methods help you perform specific tasks with certain types of data. Using these methods makes it easier for you to create robust applications. Of all the methods provided by objects, these methods are the most common and likely the most important for many situations:

  • length(): Returns the number of something. For example, when working with a string, length() returns the number of characters in the string. Likewise, when working with an array, length() returns the number of elements in the array. This method also appears as a property in some cases.

  • toLocaleString(): Outputs the value of an object as a locale-specific string. For example, when the locale uses a comma for the decimal point, the viewer will see a comma, rather than a period, even if you use a period in your code. It’s essential that you provide this support for people from other countries that visit your site.

    Simply displaying data with locale in mind doesn’t perform any data conversion. For example, the strings you create won’t suddenly appear in French if you natively speak English. There’s nothing magic about locale-specific methods. All that these methods do is change the presentation of the data as you provide it.

  • toString(): Outputs the value of the object as a string. This method is often used for display purposes.

  • valueOf(): Returns a native version of the object value. You need this method in situations where an object could cause problems. For example, when saving data to disk, you want the value, not the object, stored.

JavaScript also includes the concept of global functions. These functions are available without regard to any object from any place you use JavaScript on a page. The following list provides an overview of the most common global functions:

  • decodeURI(): Decodes a Uniform Resource Identifier (URI).

    Encoding replaces whitespace characters, such as a space, with whitespace equivalent values, such as %20. In addition, Unicode characters that would normally cause parsing problems, such as those with diacritical marks, are replaced with their Unicode equivalents.

  • decodeURIComponent(): Decodes a URI component, rather than the entire URI.

    URIs normally have between three or five standard components:

    • Protocol: The set of transport rules used to access the resource, such as HTTP, HTTPS, FTP, SMTP, or NNTP.

    • Host: The name of the server used to provide access to the resource, such as blog.johnmuellerbooks.com.

    • Port number: The port used to access the resource. In general, you don’t provide this component because most sites use standard ports, which are assumed by the browser. For example, HTTP relies on port 80 for communication. When the server uses port 80, you don’t need to include the port number as part of the URI.

    • Path: The fully defined location of the resource on the server. In some cases, you don’t provide a path, which means that the server provides the resource found on the default path.

    • Query string: Name and value pairs that define additional information required to obtain the resource you want on the server.

  • encodeURI(): Encodes a URI.

  • encodeURIComponent(): Encodes a URI component rather than the entire URI.

  • escape(): Encodes a string using the same techniques used for a URI. For example, escape("This string is encoded!") outputs This%20string%20is%20encoded%21.

  • eval(): Accepts a string that contains a script and then executes the string content as a script. Many developers use this function to create self-modifying applications that provide good flexibility.

    Using the eval() function in your JavaScript application is an incredibly bad idea for the following reasons:

    • Using evaluated code opens your application to potential security problems through injection attacks.

    • Debugging evaluated code is incredibly hard because none of the normal debugging tools will work.

    • Evaluated code runs more slowly because the browser can’t compile and then cache it.

  • isFinite(): Returns true when a value is a finite, legal number.

  • isNaN(): Returns true when a value is an illegal number.

  • Number(): Changes an object’s value to a native number.

  • parseFloat(): Parses a string and returns a floating point number.

  • parseInt(): Parses a string and returns an integer.

  • String(): Converts an object’s value to a string. This function provides the same output as the toString() method provided by most objects.

  • unescape(): Decodes an encoded string by using the same techniques used for a URI.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Create Cascading Style Sheets (CSS) Simply and Easily
How to Create a New JavaScript File in Komodo Edit
How to Use CSS3 Image Effects
How to Create Tanks in Your HTML5 Game
How to Plan Your HTML5 Game

Inside Dummies.com