Help re Blocking Text Selection

Discussion in 'Javascript' started by Martin Rinehart, Dec 7, 2008.

  1. 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.
     
    Martin Rinehart, Dec 7, 2008
    #1
    1. Advertising

  2. Martin Rinehart

    David Mark Guest

    On Dec 7, 2:29 pm, Martin Rinehart <> wrote:
    > 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]
     
    David Mark, Dec 8, 2008
    #2
    1. Advertising

  3. On Dec 8, 4:32 am, David Mark <> wrote:
    > 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.
     
    Martin Rinehart, Dec 8, 2008
    #3
  4. Martin Rinehart

    Henry Guest

    On Dec 8, 3:17 pm, Martin Rinehart wrote:
    > On Dec 8, 4:32 am, David Mark wrote:
    >> 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.
     
    Henry, Dec 8, 2008
    #4
  5. Henry wrote:
    > 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';
     
    Martin Rinehart, Dec 9, 2008
    #5
  6. Martin Rinehart

    David Mark Guest

    On Dec 9, 8:24 am, Martin Rinehart <> wrote:
    > Henry wrote:
    > > 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';


    That is hardly identical.
     
    David Mark, Dec 9, 2008
    #6
  7. Martin Rinehart

    Henry Guest

    On Dec 9, 1:24 pm, Martin Rinehart wrote:
    > Henry wrote:
    >> 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';


    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.
     
    Henry, Dec 9, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendra Gunawan
    Replies:
    1
    Views:
    12,597
    Allan Herriman
    Apr 8, 2004
  2. Andrew Crowe
    Replies:
    1
    Views:
    4,478
    Andrew Crowe
    Sep 13, 2004
  3. Andre Kelmanson

    blocking i/o vs. non blocking i/o (performance)

    Andre Kelmanson, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    941
    Valentin Tihomirov
    Oct 12, 2003
  4. nukleus
    Replies:
    14
    Views:
    838
    Chris Uppal
    Jan 22, 2007
  5. Loebb
    Replies:
    0
    Views:
    204
    Loebb
    Feb 23, 2004
Loading...

Share This Page