Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 744983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:02:29+00:00 2026-05-14T09:02:29+00:00

I would like to specify that firefox select a range. I can do this

  • 0

I would like to specify that firefox select a range. I can do this easily with IE, using range.select();. It appears that FFX expects a dom element instead. Am I mistaken, or is there a better way to go about this?

I start by getting the text selection, converting it to a range (I think?) and saving the text selection. This is where I’m getting the range from initially:

    // Before modifying selection, save it
    var userSelection,selectedText = '';
    if(window.getSelection){
        userSelection=window.getSelection();
    }
    else if(document.selection){
        userSelection=document.selection.createRange();
    }
    selectedText=userSelection;
    if(userSelection.text){
        selectedText=userSelection.text;        
    }
    if(/msie|MSIE/.test(navigator.userAgent) == false){
        selectedText=selectedText.toString();
    }
    origRange = userSelection;

I later change the selection (successfully). I do so by range in IE and by a dom ID in ffx. But after I do that, I want to set back the selection to the original selection.

This works like a charm in IE:

setTimeout(function(){
    origRange.select();
},1000);

I would like to do something like this in FFX:

var s = w.getSelection();
setTimeout(function(){
    s.removeAllRanges();
    s.addRange(origRange);
},1000);

Unfortunately, FFX has not been cooperative and this doesn’t work. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T09:02:29+00:00Added an answer on May 14, 2026 at 9:02 am

    The short answer is: IE and other browsers differ in their implementations of selecting text using JavaScript (IE has its proprietary methods). Have a look at Selecting text with JavaScript.

    Also, see setSelectionRange at MDC.

    EDIT: After making a little test case, the problem becomes clear.

    <!DOCTYPE html>
    <html>   
      <head> 
        <meta charset="UTF-8">
        <title>addRange test</title>
        <style>
          #trigger { background: lightgreen }
        </style>
      </head>
      <body> 
        <p id="test">This is some (rather short) text.</p>
        <span id="trigger">Trigger testCase().</span>
        <script>
    var origRange;
    
    var reselectFunc = function () {
        var savedRange = origRange;
        savedRange.removeAllRanges();
        savedRange.addRange(origRange);
    };
    
    var testCase = function () {
        // Before modifying selection, save it
        var userSelection,selectedText = '';
    
        if(window.getSelection){
            userSelection=window.getSelection();
        }
        else if(document.selection){
            userSelection=document.selection.createRange();
        }
        selectedText=userSelection;
        if(userSelection.text){
            selectedText=userSelection.text;
        }
        if(/msie|MSIE/.test(navigator.userAgent) === false){
            /* you shouldn't do this kind of browser sniffing,
               users of Opera and WebKit based browsers
               can easily spoof the UA string */
            selectedText=selectedText.toString();
        }
        origRange = userSelection;
    
        window.setTimeout(reselectFunc, 1000);
    };
    
    window.onload = function () {
        var el = document.getElementById("trigger");
        el.onmouseover = testCase;
    };
        </script>
      </body>
    </html>
    

    When testing this in Firefox, Chromium and Opera, the debugging tools show that after invoking removeAllRanges in reselectFunc, both savedRange and origRange are reset. Invoking addRange with such an object causes an exception to be thrown in Firefox:

    uncaught exception: [Exception…
    “Could not convert JavaScript argument
    arg 0 [nsISelection.addRange]”
    nsresult: “0x80570009
    (NS_ERROR_XPC_BAD_CONVERT_JS)”
    location: “JS frame ::
    file:///home/mk/tests/addrange.html ::
    anonymous :: line 19” data: no]

    No need to say that in all three browsers no text is selected.

    Apparently this in intended behaviour. All variables assigned a (DOM)Selection object are reset after calling removeAllRanges.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.