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

  • SEARCH
  • Home
  • 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 901515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:33:59+00:00 2026-05-15T15:33:59+00:00

I use this simple Javascript code to retrieve text that was selected on a

  • 0

I use this simple Javascript code to retrieve text that was selected on a webpage:

var userselection = window.getSelection(); //user selection will be a Selection-Object
var rangeObject = userselection.getRangeAt(0); 

The code works fine if text is selected. If I just click in the text that could be selected I get a range object too, it is collapsed as expected.

But if I don’t select nor click, Firefox throws this error:

uncaught exception: [Exception... "Component returned failure code: 0x80070057
(NS_ERROR_ILLEGAL_VALUE)  [nsISelection.getRangeAt]" nsresult: "0x80070057 
(NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: file:///home/jan/Webprojekte/test-range
selection.html# :: who :: line 16" data: no]

Is there a clean way to prevent the error but still notificate the user that no text was selected?

  • 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-15T15:34:00+00:00Added an answer on May 15, 2026 at 3:34 pm

    Yes: check the selection’s rangeCount property first. If it’s zero, don’t call getRangeAt().

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

Sidebar

Related Questions

I use this simple script : <body> <script type=text/javascript> function mouseOver() { document.getElementById(img1).src =images/p2.jpg;
In my open-source plain C code I use this simple structure to read and
Am I missing something? I use this simple code <a name=fb_share type=button href=https://www.facebook.com/sharer.php?u=123.abc.comt=TEst>share on
I use this code: http://blogswizards.com/plugin-development/sliding-boxes-and-captions-with-jquery On a simple gallery site I am building. Specifically
I have simple JavaScript code that is using the Ajax API for fetching a
I've been fighting with this simple piece of code forever. I'm trying to use
When I try to use this in my Javascript prototype like so: Array.prototype.sample =
In order to delete all .svn files/folders/subfolders in myfolder I use this simple line
I know how to get the scrollTop of a page, I use this simple
I'm trying to compile this simple program to start learning how to use timers:

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.