spacer

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

home / programming / javascript / professional / chap4 / 2 1234
[next]
Developer News
SaaS Tool Offers Custom Database Development
MicrosoftÂ’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear

Professional JavaScript

JavaScript Entities

An HTML entity reference is a bit of HTML syntax that lets difficult-to-type characters be represented. Examples are a non-breaking space(&nbsp;) and an accented A(&Aacute;). JavaScript entities are an extension to this syntax. This example draws two horizontal lines, but cleverly computes the lengths of the lines (which are normally specified as an HTML attribute) from some JavaScript variables and a bit of math. You couldn't do this without JavaScript, as either a <HR> tag has a fixed WIDTH attribute in plain HTML or none at all. The first line shows total units sold, the second the percent of a batch of 1000 units sold:

<HTML><HEAD>
  <SCRIPT>
    var pixels_per_unit = 25.4;
    var total_units = 8;
  </SCRIPT>
</HEAD>
<BODY>
  Bar graph showing number of units:<BR>
  <HR ALIGN=LEFT SIZE=10 WIDTH="&{pixels_per_unit * total_units};"><BR>

  Bar graph showing units as a percentage of 1000:<BR>
  <HR ALIGN=LEFT SIZE=10 WIDTH="&{(pixels_per_unit*total_units/1000) * 100};%">

</BODY></HTML>

It's the values of the WIDTH attribute of the <HR> tags that contains the JavaScript entities. Everything between the curly braces is evaluated as a script and the result replaces the entity at the end, same as an HTML entity. Unlike HTML entity references, these can only be used in the value part of an HTML tag attribute, as shown, not in normal HTML content. Notice in the second <HR> that the result of the JavaScript entity is concatenated with '%'. Whatever other characters are in the value part of the tag attribute will concatenate with the JavaScript entity's value. Any JavaScript expression can be used between the braces. JavaScript entities are of limited use. They are supported only in Navigator 3.0+, and in fact the HTML 4.0 standard advises against persisting with the use of this feature. It's there if you want it.

JavaScript URLs

A JavaScript URL is a special kind of URL. Mostly, URLs contain some kind of address that leads to a file, an e-mail account, or a terminal session when that hypertext link is clicked on. JavaScript URLs are different in that they don't retrieve or send anything, except as a side effect. What they do instead is cause the immediate execution of a bit of script, without the overhead of any request to a web server.

JavaScript URLs only occupy a single line, so if they contain more than one JavaScript statement, semi-colons should be used. There are three places to put JavaScript URLs.

Interactive

All Netscape browsers, plus Internet Explorer 4+ support this functionality.

javascript:Math.PI * radius * radius
javascript:alert(top.name)
javascript:if ( top.page_number ) { alert(top.page_number.hits); }
The web browser user can type JavaScript URLs directly into the browser in the same way as for any URL. You can use the browser as a simple calculator, as in the first line above. A more common use is as a debugging aid, shown in the other two lines ('alert()' is a browser host function–more on that later). Typing JavaScript URLs interactively lets you examine any hidden (or not so hidden) contents of an HTML page, such as variables or properties. The URL has access to all the pages in the current window if frames are present. Here is a simple example based on this HTML source:
<HTML>
<HEAD><TITLE>Test Page</TITLE></HEAD>
<BODY>Test content</BODY>
</HTML>

From this next screen shot you can see the JavaScript URL typed, the content and title of the page, and the small alert box window that is the result of the JavaScript URL. Note that the title of the main window agrees with the data the URL dug out of the document and displayed in the small window:

1

There's a lot happening in the above example. For now just accept that JavaScript URLs can get information out of HTML documents. The Windows and Frames chapter describes the rest of what's going on here.

If the URL is just typed as follows: 'javascript:' a special interpreter window appears that allows plain JavaScript to be typed in and executed straight away without the javascript: prefix. Here's a picture of the Netscape JavaScript interactive interpreter:

2

To get this display, first we typed 'javascript:' where URLs are normally typed. Then, in order to get the output shown, we typed the following into the input box at the bottom:

3 + 4
Math.PI
"Testing" + 1 + "2" + 3
home / programming / javascript / professional / chap4 / 2 1234
[next]

Copyright 1999 (1st Edition) and 2001 (2nd Edition) Wrox Press Ltd. and

JupiterOnlineMedia

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

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
How to Create an Ajax Autocomplete Text Field: Part 6 · Software Engineering for Ajax · Perl Pragma Primer
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Using File Virtualization for Disaster Recovery · VoIP Security: SIP—Versatile but Vulnerable · Around the World in 80 Nodes


Created: February 12, 2001
Revised: March 5, 2001


URL: http://webreference.com/programming/javascript/professional/chap4/