[Javascript] No right click on images
Nick Fitzsimons
nick at nickfitz.co.uk
Wed Sep 14 06:24:31 CDT 2005
> Hi Alan,
>
> You could try something like this
>
> for(var i=0; i<document.getElementsByTagName('IMG').length;i++){
> document.getElementsByTagName('IMG').oncontextmenu = function
> (){return false}
> }
>
> Laurent
FWIW, those repeated call to document.getElementsByTagName are very
inefficient, and there's no reason for the closure over the function.
(Also, you missed off the indexing into the array :-)
A faster method in terms of execution speed, which is also less likely to
encounter problems from memory leaks, is to get all the images outside the
loop and add a function reference:
function cancelMenu() {
return false;
}
window.onload = function() {
var allImages = document.getElementsByTagName("img");
for (var i = 0; i < allImages.length; i++) {
allImages[i].oncontextmenu = cancelMenu;
}
}
(Note that the above clobbers any other onload handlers, so adapt it
accordingly for your circumstances.)
HTH,
Nick.
--
Nick Fitzsimons
http://www.nickfitz.co.uk/
More information about the Javascript
mailing list