Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make text nonselectable

Status
Not open for further replies.

jtaal

Technical User
Jan 17, 2004
23
NL
Does anyone know how to make text unselectable?
I want to make a DIV layer so that the text can't be selected...
 
Put it in an image, or in a PDF file. HTML is a text-based language, even if there was a way of stopping people from selecting text they could still View Source and select from there.

Fundamentally, if you don't want people to be able to copy your work, don't put it on the internet.

-- Chris Hunt
 
Thanks guys,
I didn't explain what it was for, actually it's a performance issue (not to protect data). The DIV has an OnClick event, and the text inside is short and descriptive, and i don't want it to be selected because it looks ackward. I tried blurring, but selected text doesn't unselect on blur.
The option of putting it into an image is also not possible because i use some cgi script to generate the HTML...
 
How about putting another div on top and moving the onclick from the div below to the new div above? You could use zindex for this, or just put the new div below the original one on the page.

Just a thought!

Jeff
 
If i'd create a spacer IMG with exactly the same width and height in that DIV, it might just work.
This is gonna be a pain, because all de DIVs can change their horizontal sizes :-(
 
Code:
<style>
#bottom {
 position: relative;
 width: 200px;
 height: 200px;
 z-index: 1;
}

#bottom img {
 position: absolute;
 width: 100%;
 height: 100%;
 z-index: 2;

</style>

<div id=&quot;bottom&quot;>
 <img src=&quot;/tcweb/images/spacer.gif&quot; width=&quot;100%&quot; height=&quot;100%&quot; />
 lots of text, lots of text, lots of text, lots of text, lots of text, lots of text, lots of text, 
</div>
This basically works in IE6 and Mozilla, though you can still do simple select by just starting outside the div area to begin with. Another thing is just viewing the source and grabbing the text like Chris noted.
 

Or you could use this method:

Code:
<html>
<head></head>
<body>
<div>Selectable text</div>
<div onSelectStart=&quot;return(false);&quot;>Unselectable text</div>
</body>
</html>

Hope this helps!

Dan
 
Try this, I stumbled across it when viewing the source of someone's site one time...don't remember where...:
Code:
<script language=&quot;JavaScript1.2&quot;>

//Disable select-text script (IE4+, NS6+)- By Andy Scott
//Exclusive permission granted to Dynamic Drive to feature script

function disableselect(e){
return false
}

function reEnable(){
return true
}

//if IE4+
document.onselectstart=new Function (&quot;return false&quot;)

//if NS6
if (window.sidebar){
document.onmousedown=disableselect
document.onclick=reEnable
}
</script>
<script language=&quot;JavaScript1.2&quot;>
//Gradual-Highlight image script- By Dynamic Drive
//For full source code and more DHTML scripts, visit [URL unfurl="true"]http://www.dynamicdrive.com[/URL]
//This credit MUST stay intact for use
function high(which2){
theobject=which2
highlighting=setInterval(&quot;highlightit(theobject)&quot;,50)
}
function low(which2){
clearInterval(highlighting)
if (which2.style.MozOpacity)
which2.style.MozOpacity=0.3
else if (which2.filters)
which2.filters.alpha.opacity=25
}
function highlightit(cur2){
if (cur2.style.MozOpacity<1)
cur2.style.MozOpacity=parseFloat(cur2.style.MozOpacity)+0.1
else if (cur2.filters&&cur2.filters.alpha.opacity<100)
cur2.filters.alpha.opacity+=10
else if (window.highlighting)
clearInterval(highlighting)
}
</script>

Of course, as we all know, it's useless if the user has JavaScript turned off...



Hope This Helps!

Ecobb

&quot;Alright Brain, you don't like me, and I don't like you. But lets just do this, and I can get back to killing you with beer.&quot; - Homer Simpson
 
Oops! Sorry, I pasted WAY too much stuff! Here ya go:
Code:
<script language=&quot;JavaScript1.2&quot;>

//Disable select-text script (IE4+, NS6+)- By Andy Scott
//Exclusive permission granted to Dynamic Drive to feature script

function disableselect(e){
return false
}

function reEnable(){
return true
}

//if IE4+
document.onselectstart=new Function (&quot;return false&quot;)

//if NS6
if (window.sidebar){
document.onmousedown=disableselect
document.onclick=reEnable
}
</script>


Hope This Helps!

Ecobb

&quot;Alright Brain, you don't like me, and I don't like you. But lets just do this, and I can get back to killing you with beer.&quot; - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top