Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2005
    Location
    Latina (Italy)
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Internet Explorer TextRange actual position

    Hi y'all!!

    Given a TextRange (so in IE environment), is it possible to determine which are its current delimiters into the node?
    I mean, using
    Code:
    moveStart(...)
    or
    Code:
    moveEnd(...)
    methods I'm able to drag them... but how can I count the number of characters before the textrange selection?

    Suppose a client is selecting a text portion: how can I know what's the start-offset without moving it?

    In Mozilla environment, I'd solve my problem simply reading the
    Code:
    Range.startOffset
    attribute. How can I afford something similar with IE?

    Thanx in advance!!

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,496
    Thanks
    3
    Thanked 500 Times in 487 Posts
    Code:
    <!DOCTYPE html
      PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml"
      xml:lang="en" lang="en">
    
    <head>
      <title>Web Design</title>
      <meta name="author" content="Mr. Smith, I presume" />
      <meta name="description"  content="web" />
    
    
    <style type="text/css">
    <!--
    body{
        background: buttonface;
    }
    .box_style{
        border-style: inset;
        border-width: 3px;
        width: 50%;
        height: 100px;
        overflow: auto;
        background-image:
    url(<a href="http://www.grsites.com/folders/my_backgrounds/misc180.gif" target="_blank">http://www.grsites.com/folders/my_b...nds/misc180.gif</a>);
    }
    .buttons{
        display: block;
        margin-top: 25px;
        margin-bottom: 0;
    }
    
    -->
    </style>
    
    
    <script language="javascript">
    <!-- Hide from browsers lacking javascript
    function findCursor(){
    
    var oCursor=document.selection.createRange();
    oCursor.collapse(true);
    oCursor.text="";
    }
    window.onload = initialize;
    
    function initialize(){
        //Assign a functon to the button's onclick event handler:
        document.onclick = get_range;
    }
    
    function get_range(){
        var ta_2 = document.f.ta2; //shorten the reference to ta2
    
        if(document.selection && document.selection.createRange){//then IE like broswers
            var oRange = document.selection.createRange();//creates a TextRange object encompassing the selected text.
    
            ta_2.value = oRange.text; //puts the selection into the textarea ta_2
        }
        else if(window.getSelection){//then NN like browsers
            var sText=window.getSelection();
            ta_2.value = sText;
        }
        else {
            alert("example won't work on your browser");
        }
    
    }
    // End hiding -->
    </script>
    </head>
    <body>
    
    <form name="f" method="post" action="">
     fffffffffffffffffffffffffffffff22222222222222wwwwwwwww
    <div name="ta1" class="box_style" onfocus="findCursor()" >Select some text in this box.</div>
    <input type="button" id="b1" name="b1" class="buttons" value="Display selected text"/>
    <textarea name="ta2" class="box_style" onfocus="findCursor()" ></textarea>
    </form>
    
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Sep 2005
    Location
    Latina (Italy)
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for you post vwphillips! But this script shows me which is the selected text, while I'd like to know which is the current position (for example, if my cursor/caret is on the 5th character, I'd like to know how to display "5")... is there a way to do this?

    Thanx again


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •