Help re Blocking Text Selection

  • Thread starter Martin Rinehart
  • Start date
M

Martin Rinehart

I've got generic drag code working but there's one issue: as you drag,
text under the dragging object is getting selected. It gets pretty
ugly.

An onselectstart() that simply returns false will prevent selection,
but I'm not sure that I really want to walk the entire DOM tree
attaching an onselectstart() to every element of the page. Is there a
better way?

The drag code so far hits the scales at 1.5kB. I'd like to keep it
that way.
 
D

David Mark

I've got generic drag code working but there's one issue: as you drag,
text under the dragging object is getting selected. It gets pretty
ugly.
Yes.


An onselectstart() that simply returns false will prevent selection,

A what?
but I'm not sure that I really want to walk the entire DOM tree
attaching an onselectstart() to every element of the page. Is there a
better way?

Anything would be better than that. Whatever event(s) you suppress (I
don't think I've ever used selectstart), do it at the document level.

[snip]
 
M

Martin Rinehart

Anything would be better than [walking the DOM tree].  Whatever event(s) you suppress do it at the document level.

"document.onselectstart = function () { return false; }" is just one
of the things I've tried that doesn't work.
 
H

Henry

Anything would be better than [walking the DOM tree].
 Whatever event(s) you suppress do it at the document level.

"document.onselectstart = function () { return false; }" is just
one of the things I've tried that doesn't work.

One of the things you may have noticed over the past couple of days is
that the group's FAQ has things to say about the usefulness of
assertions along the lines of "doesn't work". In what sense does this
not work? It works fine on, for example, IE 6. It never was going to
do much on browsers that do not have selectstart events.
 
M

Martin Rinehart

Henry said:
One of the things you may have noticed over the past couple of days is
that the group's FAQ has things to say about the usefulness of
assertions along the lines of "doesn't work". In what sense does this
not work? It works fine on, for example, IE 6. It never was going to
do much on browsers that do not have selectstart events.

Being more precise, it did not work in Opera on KDE. What does work,
in Opera on KDE is:

<body onselectstart='return false' onmousedown='return false'>

What did not work, in Opera on KDE, was the seemingly identical JS:

document.body.onselectstart='return false';
document.body.onmousedown='return false';
 
D

David Mark

Being more precise, it did not work in Opera on KDE. What does work,
in Opera on KDE is:

<body onselectstart='return false' onmousedown='return false'>

What did not work, in Opera on KDE, was the seemingly identical JS:

document.body.onselectstart='return false';
document.body.onmousedown='return false';

That is hardly identical.
 
H

Henry

Being more precise, it did not work in Opera on KDE. What
does work, in Opera on KDE is:

<body onselectstart='return false' onmousedown='return false'>

What did not work, in Opera on KDE, was the seemingly
identical JS:

document.body.onselectstart='return false';
document.body.onmousedown='return false';

The "on..." properties of elements are expecting to be assigned
references to function objects. A few browsers might react to a string
value being assigned to those properties by converting the string into
a function but many will not, so it is defiantly not something you
should ever do if you want a cross-browser outcome.

A possible source of confusion in this area is the way browsers handle
the values of the intrinsic event attributes in the mark-up. They use
those as the source code for the body of functions they create
themselves internally, so given " <body onmousedown='return false'>"
the browser takes the "return false" and uses it as the body of a
function that it creates and assigns to the - onmousedown - property
of the corresponding element in the DOM. The equivilent of a
javascript program performing:-

document.body.onmousedown = function(event){
return false
};

(except that IE will not include the - event - parameter in the
functions it creates (and Chrome exposes the scope chain augmentation
mechanism it uses for these functions)).

This can be illustrated by using intrinsic event attributes in HTML
and then examining the corresponding properties of the DOM elements
with scripts. I.E.:-

<html>
<head>
<title></title>
</head>
<body>
<input type="button" value="test" onclick="alert(this.onclick);">
</body>
</html>

- where clicking the button shows the value of the element's - onclick
- property as type-converted into a string, which clearly shows that
the value is a function object (with a body that corresponds with the
attribute value from the mark-up), and as no javascript code created
that function object the browser must have done so itself.

I don't think that Opera browsers support the - selectstart - events
(they certainly did not used to) so I suppose you are saying that
cancelling the default action of a mosuedown event is effective at
preventing selections, which seems reasonable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top