homepage Welcome to WebmasterWorld Guest from 54.211.7.174
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
onselectstart="return false" and XHTML validation.
I want to use "onselectstart" but it's not attribute of XHTML
iProgram




msg:3054098
 7:22 am on Aug 21, 2006 (gmt 0)

I want to use onselectstart="return false" to prevent copycat from copying my content. For example:

<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]

 

RonPK




msg:3054340
 1:02 pm on Aug 21, 2006 (gmt 0)

Have a look at the attachEvent [msdn.microsoft.com] method. In your case, something like this might do:

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]

iProgram




msg:3054521
 3:15 pm on Aug 21, 2006 (gmt 0)

Thank you it works great!

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved