/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Validate code

I am using this line of code to prevent right-click (and subsequently [I]save image[/I]):

[CODE]<img src=”img/myimage.jpg” onMouseDown=”if (event.preventDefault) event.preventDefault()” oncontextmenu=”return false;” >[/CODE]

It’s a standard code, except that the browser warning is suppressed. And it works fine.
Unfortunately, this is not valid coding. So, is there a way to fix it?

Oh, and please no discussions about preventing theft from websites. I know nothing really works, I just want to deter the majority of web users.

Thanks in advance.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@twseitexAug 08.2011 — Hi,

check event object an method .preventDefault()

for Microsoft Interne Explorer see http://msdn.microsoft.com/en-us/library/ms535863.aspx

if(event.preventDefault)

this check a pointer like if(event.preventDefault!=null)
Copy linkTweet thisAlerts:
@PrisauthorAug 16.2011 — Thanks twseitex, will look into your solution asap.
Copy linkTweet thisAlerts:
@PrisauthorSep 03.2011 — Found a jQuery snippet that works nicely:[CODE]<script type="text/javascript">
$('img').ready(function(){
$('img').bind("contextmenu",function(e){
return false;
});
});
</script>[/CODE]


Note: it comes from here and I adapted it. The original code disables right-click completely, but I do want visitors to be able to open links in new tabs or copy text. So here you go, disable right-click on images with jQuery.
×

Success!

Help @Pris spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...