HTML and CSS Reference
In-Depth Information
TABLE 4-3 Pros and cons of embedding JavaScript code in HTML pages
Pro
Con
The page has no dependencies and is self-contained.
As a developer, you don't have to look in several
places to find out things specific to a page.
The page is larger and takes more time to download.
The script code cannot be used outside the page that
contains it.
Restructuring the code to minimize duplicated func-
tionalities is compromised by the fact that no code
reuse is possible outside the page.
Script code cannot be cached by browsers separately
from the page.
In addition, there's another point to consider: with embedded code, any changes to the script are
immediately visible. You just save the file and refresh the browser. But when the script file is linked as
an external resource (more on this in a moment), then you need to play some tricks to ensure that
changes are immediately visible and not hidden by cached copies of the same file.
This aspect is certainly important, but it doesn't affect released applications. It is an important
aspect, but only during the development phase.
External files
As an alternative to embedding code in a script element, you can use the same script element to link a
JavaScript file as an external resource. Here's how to do it:
<script src="http://..." type="text/javascript" />
The pros of using externally linked JavaScript files are exactly opposite to the cons of using embed-
ded script. This is the way to go; or, at least, using externally linked files should always be the first
option you consider in general web development and in Windows 8 development.
jQuery and Windows 8
At present, most JavaScript development is done with the immensely popular jQuery library.
You can download the library from http://jquery.com .
Typical web developers use the jQuery library to unobtrusively bind handlers to events and to
detect page-level events, such as the event that indicates the page is ready for display. In addition,
web developers use jQuery to query for ad hoc subsets of page elements. With reference to the
previous chapter on CSS, you could say that jQuery offers a syntax that mimics the syntax of CSS
selectors. In reality though, jQuery selector syntax is even richer than CSS standards.
For developers who have a strong web background, the thought of writing HTML pages with-
out using jQuery (or other popular JavaScript libraries, such as, knockout.js ) may be nonsense.
 
Search WWH ::




Custom Search