• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

The TextRange Object

The Internet Explorer TextRange object enables you to work with a range of text in a web page. In fact, we’ve already seen an example using text ranges in Chapter 9, “Using the document and body Objects”—that example enabled you to access text the user selected with the mouse in a web page. In the Internet Explorer, you use the document.selection property, then create a text range from that property using the createRange method, and use the text range’s text property to find the selected text. Here’s what that looked like (Listing 09-07.html on the web site):

<HTML> 
    <HEAD> 
        <TITLE>Reading Selected Text</TITLE> 
        <SCRIPT LANGUAGE="JavaScript"> 
            <!--
            function getSelected() 
            { 
                if (navigator.appName == "Microsoft Internet Explorer") {
					docum+ent.form1.text1.value = document.selection.createRange().text 
                } 
                if(navigator.appName == "Netscape") { 
                    document.form1.text1.value = document.getSelection() 
                } 
            } 
            // --> 
        </SCRIPT> 
    </HEAD> 

    <BODY ONMOUSEUP="getSelected()"> 
        <H1>Reading Selected Text</H1> 
        Select some of this text! 
        <BR> 
        <FORM NAME="form1"> 
            You selected: <INPUT TYPE="TEXT" NAME="text1"> 
        </FORM> 
    </BODY> 
</HTML> 

					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint