spacer
Yehuda Shiran October 22, 2001
Selecting Text in Netscape 6
Tips: October 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Google Going Native With Chrome
Mozilla Fixes Firefox Flaws as 3.5 Release Nears
Microsoft and Novell Still Bosom Buddies

Netscpae 6 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 Netscape 6 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 Netscape 6 , 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.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
XML and PHP Simplified · Creating a ASP.NET Contact Form · Data Filtering with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Intel to Host Live Nehalem Q&A · 12 Tips to Troubleshoot Network File-Sharing · 10 Tips for Selling on Kijiji