Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 22nd, 2005, 06:11 AM
Friend of Wrox
 
Join Date: Sep 2004
Location: Tehran, Tehran, Iran.
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?

Reply With Quote
  #2 (permalink)  
Old June 22nd, 2005, 10:48 AM
joefawcett's Avatar
Wrox Author
Points: 9,731, Level: 42
Points: 9,731, Level: 42 Points: 9,731, Level: 42 Points: 9,731, Level: 42
Activity: 11%
Activity: 11% Activity: 11% Activity: 11%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,068
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)
Reply With Quote
  #3 (permalink)  
Old June 23rd, 2005, 07:34 AM
Friend of Wrox
 
Join Date: Sep 2004
Location: Tehran, Tehran, Iran.
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What about other browsers?

Reply With Quote
  #4 (permalink)  
Old June 23rd, 2005, 09:22 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 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
Reply With Quote
  #5 (permalink)  
Old June 23rd, 2005, 09:33 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 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
Reply With Quote
  #6 (permalink)  
Old June 26th, 2005, 02:24 AM
Friend of Wrox
 
Join Date: Sep 2004
Location: Tehran, Tehran, Iran.
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...

Reply With Quote
  #7 (permalink)  
Old June 27th, 2005, 10:56 AM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
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/
Reply With Quote
  #8 (permalink)  
Old June 27th, 2005, 11:07 AM
joefawcett's Avatar
Wrox Author
Points: 9,731, Level: 42
Points: 9,731, Level: 42 Points: 9,731, Level: 42 Points: 9,731, Level: 42
Activity: 11%
Activity: 11% Activity: 11% Activity: 11%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,068
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)
Reply With Quote
  #9 (permalink)  
Old August 5th, 2005, 05:04 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ondragstart is in VBA :)

Reply With Quote
  #10 (permalink)  
Old January 13th, 2008, 06:30 PM
Registered User
 
Join Date: Jun 2007
Location: , , .
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;}
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 November 30th, 2006 11:35 PM
html:errors and html:messages sparun1607 Struts 0 November 23rd, 2006 07: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 03:17 PM



All times are GMT -4. The time now is 06:41 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.