SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    document.selection not working in mozila

    Following code i modified to make it work in the firefox browser. whats going wrong. pls suugest the workarounds

    but its not working in firefox

    if (document.selection)
    {
    var objRange = document.selection.createRange();
    var sOldRange = objRange.text;

    //set this string to a small string that will not normally be encountered
    var sWeirdString = '#%~';

    //insert the weirdstring where the cursor is at
    objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

    //save off the new string with the weirdstring in it
    var sNewText = textElement.value;

    //set the actual text value back to how it was
    objRange.text = sOldRange;

    //look through the new string we saved off and find the location of
    //the weirdstring that was inserted and return that value
    for (i = 0; i <= sNewText.length; i++) {
    var sTemp = sNewText.substring(i, i + sWeirdString.length);
    if (sTemp == sWeirdString) {
    var cursorPos = (i - sOldRange.length);
    return cursorPos;
    }
    } }
    else if (window.getSelection)
    {

    /* var objRange = document.getSelection.createRange();
    var sOldRange = objRange.text;

    //set this string to a small string that will not normally be encountered
    var sWeirdString = '#%~';

    //insert the weirdstring where the cursor is at
    objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

    //save off the new string with the weirdstring in it
    var sNewText = textElement.value;

    //set the actual text value back to how it was
    objRange.text = sOldRange;

    //look through the new string we saved off and find the location of
    //the weirdstring that was inserted and return that value
    for (i = 0; i <= sNewText.length; i++) {
    var sTemp = sNewText.substring(i, i + sWeirdString.length);
    if (sTemp == sWeirdString) {
    var cursorPos = (i - sOldRange.length);
    return cursorPos;
    }




    }*/



    }
    }

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    I'm about to put my head down for a while, but Quirksmode has been the best place to learn all about text selection ranges, and how to make them work in an accessible cross-browser manner.
    Introduction to Range
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaiswati View Post
    /* var objRange = document.getSelection.createRange();
    ...
    }*/
    No wonder it's not working, since you've commented out everything!

    There's an error in the code you commented out (the line I quoted above). document.getSelection is a method, not a property, and it returns a selection object upon which you can call the createRange() method. So you need to insert a couple of brackets in that line (and remove the commenting, of course).

    Code JavaScript:
    var objRange = document.getSelection().createRange();
    Birnam wood is come to Dunsinane

  4. #4
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried as said by AutisticCuckoo and following example also



    <div onmouseup="alert(getSelectionRange());">Hallo Welt</div>
    <input type="text" onmouseup="alert(getSelectionRange());" value="Hallo
    Welt"/>

    <script>
    function getSelectionRange() {
    var rng=null;
    if(window.getSelection) { // Gecko
    rng=window.getSelection();
    if(rng && rng.rangeCount && rng.getRangeAt) {
    rng=rng.getRangeAt(0);
    }
    } else if(document.selection && document.selection.type=="Text") { // IE
    rng=document.selection.createRange().text;
    }
    return rng;
    }
    </script>

    still it wont give the selection in mozila but works fine in IE
    Somebody please help me out

  5. #5
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried the following function which also dont give the selection in mozila

    function getSelText() {
    var txt = '';
    if (window.getSelection) {
    txt = window.getSelection();
    alert(window.getSelection()); //also giving the blank selection
    }
    else if (document.getSelection) {
    txt = document.getSelection();
    alert('H1');
    }
    else if (document.selection) {
    txt = document.selection.createRange().text;
    alert('H2');
    }
    else return;
    document.aform.selectedtext.value = txt;
    }

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works in Firefox (and Opera, Chrome, etc.):
    Code JavaScript:
    var txt = window.getSelection().getRangeAt(0).toString();
    alert(txt);
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

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