473,327 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Preventing text selection (Mozilla Firefox)

Hi,

I'd need some way to prevent users from selecting text in a web page. I'm only interested in Mozilla Firefox family browsers.

I came with this javascript snippet so far:

onmousedown = "return false;"

This works, but it has the problem of disabling inputs too, so the user won't be able to enter them with his pointer.

Does anybody know of a better way to avoid text selection in the whole page while still keeping the other elements working?

Thanks a lot
Nov 7 '10 #1

✓ answered by hsriat

Not the perfect solution, but try this...
Expand|Select|Wrap|Line Numbers
  1. function disableTextSelect(node) {
  2.   node = node || document.body;
  3.   if (node.nodeType != 1) return;
  4.   if (node.getElementsByTagName('input').length + node.getElementsByTagName('textarea').length) {
  5.     for (var i = 0; i < node.childNodes.length; i++) {
  6.       if (!node.childNodes[i].nodeName.match(/^(input|textarea)$/i)) disableTextSelect(node.childNodes[i]);
  7.     }
  8.   }
  9.   else {
  10.     node.onmousedown = function() {
  11.       return false;
  12.     };
  13.   }
  14. }
  15. disableTextSelect();
Dynamically added elements may give trouble.

4 2742
Atli
5,058 Expert 4TB
Could you tell us why you want to do this? If you are trying to prevent visitors from copying your content, that's not possible. You may be able to prevent them from selecting it under normal circumstances, but that is extremely easy to work around.

However, the most reliable way I can think of is to cover whatever text you want disabled with another, empty and transparent element. That way when visitors try to select the text, they will be clicking the transparent element rather then the text below. - I say this is most reliable because it dose not require JavaScript. Can be done using HTML and CSS alone.

Your onmousedown = "return false;" method can also be applied more specifically to text elements alone. That is, you could do something like this:
Expand|Select|Wrap|Line Numbers
  1. var paragraph = document.getElementById('myParagraph');
  2. paragraph.onmousedown = function() { return false; }
  3.  
This would disable mouse interaction on the element with the ID 'myParagraph' and nothing else.

You can of course use things like the document.getElementsByTagName function to target more than a single element at a time. Just make sure the elements you target do not contain elements that visitors should be able to interact with.

Note, though, that this a JavaScript solution and can VERY easily be bypassed.
Nov 7 '10 #2
Thanks, Atli. I know it's very easy to bypass it, but it's intended to work in a controlled environment, where users are seeing html documents in a custom application with an embedded browser which offers no control over javascript execution.

I'd rather want a more general way of applying the function instead of selecting it's receivers by id because I don't know which structure the documents will have.
Nov 7 '10 #3
hsriat
1,654 Expert 1GB
Not the perfect solution, but try this...
Expand|Select|Wrap|Line Numbers
  1. function disableTextSelect(node) {
  2.   node = node || document.body;
  3.   if (node.nodeType != 1) return;
  4.   if (node.getElementsByTagName('input').length + node.getElementsByTagName('textarea').length) {
  5.     for (var i = 0; i < node.childNodes.length; i++) {
  6.       if (!node.childNodes[i].nodeName.match(/^(input|textarea)$/i)) disableTextSelect(node.childNodes[i]);
  7.     }
  8.   }
  9.   else {
  10.     node.onmousedown = function() {
  11.       return false;
  12.     };
  13.   }
  14. }
  15. disableTextSelect();
Dynamically added elements may give trouble.
Nov 8 '10 #4
Thanks, hsriat!

Your solution works fine. The only way I noticed you could actually select any text with it was dragging your mouse from an empty area to a text paragraph.

Then I modified your function a bit so to block mousedown by default on document's body and then allow inputs/textareas to accept it. The key here was the "event.stopPropagation()" function ;-)
Nov 8 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rafal Rutkowski | last post by:
Hi, Does anyone know a way to prevent IE from selecting text when a user clicks the text while holding shift/ctrl key? -- Rudy
4
by: ekimnosnews | last post by:
I'm programming a JS tile map editor for my friend's game. The map editor has different "sections" that contain different tiles. The way that it works is when you select a section it calls a JS...
4
by: Derek | last post by:
I have the following script in a page and it gets an error in IE 6. Says something about an invalid argument but the line number doesn't help since I can't see the javascript code when viewing...
2
by: Mark Szlazak | last post by:
The following code fails in Firefox to get at selected text in the right-side textarea. Any help would be appreciated. <html> <head> <script> var agt = navigator.userAgent.toLowerCase();...
1
by: Robert Oschler | last post by:
Can anyone give me a Javascript snippet that will get the currently selected text in a TEXTAREA element? The code I have only works in IE. Thanks.
0
by: BACON | last post by:
I'm just starting the process of reorganising my modest little website and cleaning up all the HTML, and the logical place to begin was with the homepage. I made a simple little ASP.NET control...
27
by: rsteph | last post by:
I've got a site that appears properly in both IE and Netscape for has all sorts of appearance issues in Mozilla Firefox. I've looked around for some points on the web that help explain the issue, but...
2
by: wolfgang.schwarz | last post by:
hi, is there a way of getting the position and size of a text node in Firefox without altering the DOM? For example, suppose I have a document fragment like this: <div> textnode1 <img>...
1
by: xtremebass | last post by:
Hello Bytes, i have a calender program which is created by using Javascript. when i execute that program using Internet Explorer,it works properly but when i tried in Mozilla firefox it didnt...
1
by: rbuczynski | last post by:
I've read two threads that have been closed in which a user posed the question, "Can I disable text selection using CSS?" The responses were either hopeless or critical of the inquirer. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.