Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 22nd, 2005, 06:11 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Default non-selectable html?

Hi all,
How can I make a text or table in an html document non-selectable? In other words how can I prevent an html item to be selected(highlighted blue in IE) while pressing and draging the mouse on it?Is there any style to do the job?

 
Old June 22nd, 2005, 10:48 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

In IE you can set unselectable="on" in the body tag. You could also try ondragstart="return false;" in the body element.

--

Joe (Microsoft MVP - XML)
 
Old June 23rd, 2005, 07:34 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What about other browsers?

 
Old June 23rd, 2005, 09:22 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

In Mozilla, Netscape, and Mozilla Firefox you can do something like this in a style sheet:

::-moz-selection {
   background: transparent;
   color: inherit;
}

That just makes the selection invisible, there is still a selection though.

Safari, I think, supports ::selection, therefore you can do this in Safari, and possibly Konquorer as well.

::selection {
   background: transparent;
   color: inherit;
}

Don't know though, haven't tested it.

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
 
Old June 23rd, 2005, 09:33 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

BTW, the two selection psuedo-elements cannot be grouped. For instance:

::-moz-selection, ::selection {
   background: transparent;
   color: inherit;
}

This doesn't work in Mozilla, it completely ignores the rule.

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
 
Old June 26th, 2005, 02:24 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks but I'm using the following code instead:

<html>
<head>
</head>
<body onload="window.setInterval('document.selection.emp ty()',100);">
    some text
</body>
</html>


Not sure if there's any equivalant code for other browsers...

 
Old June 27th, 2005, 10:56 AM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

What you actually want to do in Mozilla is this:

Code:
* {
    -moz-user-select:none;
}
In IE, the best way to do it is to use onselectstart:

Code:
<body onselectstart="return false">
Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
 
Old June 27th, 2005, 11:07 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Quote:
quote:Originally posted by nzakas


In IE, the best way to do it is to use onselectstart:

Code:
<body onselectstart="return false">
Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
Yes sorry nerssi, "onselectstart" was what I meant, not sure where "ondragstart" came from :)

--

Joe (Microsoft MVP - XML)
 
Old August 5th, 2005, 05:04 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ondragstart is in VBA :)

 
Old January 13th, 2008, 07:30 PM
Registered User
 
Join Date: Jun 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

1 javascript line solution:

firefox/mozilla/iexplore:

Code:
nonselectableobject.onmousedown = nonselectableobject.onselectstart = function(){return false;}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove orphaned html elements from html string pauliehaha C# 2008 aka C# 3.0 2 June 30th, 2008 09:40 AM
Custom DropDownList Control with selectable images patelhitul ASP.NET 2.0 Professional 12 December 1st, 2006 12:35 AM
html:errors and html:messages sparun1607 Struts 0 November 23rd, 2006 08:34 AM
non-selectable style? nerssi CSS Cascading Style Sheets 1 June 23rd, 2005 09:30 AM
Can you preload child html files to 1parent html? bekim Javascript How-To 4 January 22nd, 2005 04:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.