spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / prof_java / 1 To page 1current pageTo page 3To page 4To page 5
[previous][next]

Content Coordinator
Aquent
US-WA-Redmond

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Professional JavaScript for Web Developers: JavaScript in the Browser, Pt. 1

Inline code versus external files

When should you write code inline versus writing the code in an external file? Although no hard and fast rules exist about when to use either method, the general consensus is that large amounts of JavaScript should never be included inline for a number of reasons:

  • Security - Anyone can see exactly what the code is doing just by viewing the source of the page. If a malicious developer examines the code, he might find security holes that could compromise the site or application. Additionally, copyright and other intellectual property notices can be included in external files without interrupting the flow of the page.

  • Code Maintenance - If JavaScript code is sprinkled throughout various pages, code maintenance becomes a nightmare. It is much easier to have a directory for all JavaScript files so that when a JavaScript error occurs, there is no question about where the code is located.

  • Caching - Browsers cache all externally linked JavaScript files according to specific settings, meaning that if two pages are using the same file, it is only downloaded once. This ultimately means faster loading times. Including the same code in multiple pages is not only wasteful, but also increases the page size and thus increases the download time.

Tag placement
Generally speaking, it is common to place all code and function definitions in the <head/> tag of an HTML page so that the code is fully loaded and ready for use once the body is rendered. The only code that should appear within the <body/> tag is code that calls the functions defined previously.


When the <script/> tag is placed inside of the <body/> tag, the script is executed as soon as that part of the page is downloaded to the browser. This makes it possible to execute JavaScript code before the entire page is loaded. For example:

In this code, the sayHi() method is called before any text is displayed on the page, meaning that the alert pops up before the text “This is the first text the user will see.” is ever rendered. This method of calling JavaScript inside the <body/> of a page is not recommended and should be avoided whenever possible. Instead, it is recommended to use JavaScript only as an event handler in the body of a page, such as:

Here, the <input/> tag is used to create a button that calls sayHi() when clicked. The onclick attribute specifies an event handler, which is the code that responds to a given event. Events and event handlers are discussed further in Chapter 9, “All about Events.”

Note that JavaScript begins running as soon as the page begins loading, so it is possible to call function that doesn’t exist yet. In the previous example, you can cause an error by placing the original <script/> tag after the function call:

This example causes an error because sayHi() is called before it is defined. Because JavaScript is loading top-down, the function sayHi() does not exist until the second <script/> tag is encountered. Be aware of this problem and, as mentioned previously, use events and event handlers to call your JavaScript functions.

To hide or not to hide

When JavaScript was first introduced, only one browser supported it, so concern arose over how the< nonsupporting browsers would deal with the <script/> tag and the code contained within. To that end, a format was devised to hide code from older browsers (which is a phrase that can still be found in the source code of a great many Web sites on the Internet today). The following method uses HTML comments around inline code so that other browsers won’t render the code to the screen:

The first line begins an HTML comment immediately after the opening <script> tag. This works because the browser still considers the rest of that line as part of HTML, with JavaScript code beginning on the following line. Next, you see the function definition as usual. The second-to-last line is the most interesting because it starts with a JavaScript single-line comment (the two forward slashes) and then continues with the close of the HTML comment (-->). This line is still considered part of the JavaScript code, so the single-line comment notation is necessary to avoid a syntax error. However, older browsers only acknowledge the close of the HTML comment and, therefore, ignore all the JavaScript code. A browser that supports JavaScript, however, just ignores this line and continues on to the closing </script> tag.

Although this method of code-hiding was very prevalent in the early days of the Web, it is not as necessary today. Presently, most of the popular Web browsers support JavaScript, and those that don’t often are smart enough to ignore the code on their own. It is completely up to you whether you choose to use this method, but keep in mind that using external JavaScript files inside of inline code is a much easier method of hiding code from older browsers.

The <noscript/> tag

Another concern over browsers without JavaScript is how to provide alternate content. Hiding the code was part of the solution, but developers wanted a way to specify content that should appear only if JavaScript wasn’t available. The solution came in the form of the <noscript/> tag, that can contain any HTML code (aside from <script/>). This HTML code is ignored by browsers that support JavaScript and have it enabled; any browser that doesn’t support JavaScript or has it disabled renders the content of <noscript/>. For example:

In this example, the <noscript/> tag is included with a message telling the user that the browser doesn’t support JavaScript. Chapter 8, “Browser and Operating System Detection,” explains a practical way of using <noscript/>.

home / programming / prof_java / 1 To page 1current pageTo page 3To page 4To page 5
[previous][next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
Working with the DOM Stylesheets Collection · Administering RBAC in PHP 5 CMS Framework · xref: Automatic Cross Referencing Script
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Combine BottomCount() with Other MDX Functions to Add Sophistication · Creating a Daemon with Python · The Coming Voice-over-WiMAX Revolution

Created: March 27, 2003
Revised: June 20, 2005

URL: http://webreference.com/programming/prof_java/1