spacer

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

home / experts / javascript / column12


Retrieving Selections in Navigator 4.0x

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Navigator 4.0x features the getSelection() method of the document object. This method returns a string containing the text of the current selection. Its general syntax is:

document.getSelection()

The following example demonstrates the getSelection() method:

<FORM NAME="myForm">
<TEXTAREA NAME="myArea" COLS="40" ROWS="4"></TEXTAREA>
</FORM>

<SCRIPT LANGUAGE="JavaScript">
<!--

function display() {
  if (!document.getSelection)
    return;
  var str = document.getSelection();
  document.myForm.myArea.value = str;
}

if (window.Event)
  document.captureEvents(Event.MOUSEUP);
document.onmouseup = display;

// -->
</SCRIPT>

The script displays the current selection in a box. At first, it instructs the browser to capture all mouseup events. As explained in Column 9, Column 10, and Column 11, this is only required for Navigator 4.0x, so a simple object detection routine makes sure the browser is Navigator 4.0x before executing the statement.

The event processing function first assigns the selection's text to a variable named str. It then assigns that variable to the value property of the form element, so the selection is displayed in the box. Now go ahead and drag the mouse over some text. If you're using Navigator 4.0x, the selection will immediately display in the box.

The statement:

if (!document.getSelection)
  return;

terminates the function if the user is running a browser that doesn't support the document object's getSelection() method. Therefore, no error is generated on Internet Explorer 4.0x or older browsers.

Notice that the document's layout affects the value returned by the getSelection() method. For example, this page utilizes a <BLOCKQUOTE>...</BLOCKQUOTE> pair to indent the content for easier reading. If you select several lines of text, the second (and third, fourth, fifth, etc.) line's text is indented, due to the <BLOCKQUOTE> definition. In this case, the new line consists of a carriage return (\r), a line feed (\n), and five spaces. You can discover what characters make up the new line by displaying the escaped value of the string:

alert(escape(str));

The escape() function accepts a string argument and returns the same string with all non-alphanumeric characters translated into their equivalent hexadecimal code.

In Column 5 we discussed regular expressions in JavaScript. With regular expressions you can easily get rid of unnecessary characters. In our example, the \r\n combination is platform-dependent, so we'll simply delete the five spaces that follow it. Here's the alternative script:

<SCRIPT LANGUAGE="JavaScript">
<!--

function display() {
  if (!document.getSelection)
    return;
  var str = document.getSelection();
  if (window.RegExp) {
    var regstr = unescape("%20%20%20%20%20");
    var regexp = new RegExp(regstr, "g");
    str = str.replace(regexp, "");
  }
  document.myForm.myArea.value = str;
}

if (window.Event)
  document.captureEvents(Event.MOUSEUP);
document.onmouseup = display;

// -->
</SCRIPT>

First we discovered what series of characters was causing trouble, by displaying the value returned by the escape() function, as explained earlier. We immediately noticed five consecutive non-alphanumeric characters: %20%20%20%20%20. Therefore, the function unescapes those characters and assigns the value to a variable. A regular expression is then defined as an instance of the RegExp object. Notice the "g" argument. It specifies that the string should be matched against the regular expression multiple times if the pattern is found more than once in the string. The final string is displayed after replacing all occurences of five consecutive spaces with an empty string.

http://www.internet.com

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

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Crucial Triples Up With New Three-Channel DDR3 Kits · Meet the Finalists: Excellence in Technology Awards · Tealeaf Offers Insight to Mobile Customer Behavior


Created: January 29, 1998
Revised: January 29, 1998

URL: http://www.webreference.com/js/column12/getselection.html