Forum Moderators: open
<div id="my_div" onselectstart="return false">My content</div>
It works great. The only problem is my pages are no longer valid if I use it directly. So is there a way to "capture" the on_select_start event in an external.js file? Assume the id of <div> is "my_div".
[edited by: iProgram at 7:24 am (utc) on Aug. 21, 2006]
window.onload = function () {
document.getElementById('my_div').attachEvent('onselectstart', rfalse);
}
function rfalse() {return false;} Note that attachEvent and onselectstart are IE-only. In other browsers the script won't work.
[edit]fixed bugs[/edit]
[edited by: RonPK at 1:09 pm (utc) on Aug. 21, 2006]
Summary:
1. html file
<body onload="no_sel(2)">
<div id="no_sel_0" class="no_sel">my content 1</div>
...
<div id="no_sel_1" class="no_sel">my content 2</div>
...
</body>
2. js file (IE)
function rfalse()
{
return false;
}
function no_sel(l)
{
if(document.attachEvent)
{
for(var i=0; i<l; i++)
{
document.getElementById('no_sel_' + i).attachEvent('onselectstart', rfalse);
}
}
}
3. css file (Firefox)
.no_sel{
-moz-user-select:none;
}
That's all.