I want to enable right click on image only - javascript

I am using code to block right click on my blog. It's work for may website and disable ctrl+v ctrl+c-right click f12 on my webpage. But I want to enable right click on image. I don't know how to do that. But Please Help me.
var isCtrl = false;
document.onkeyup = function(e) {
if (e.which == 17)
isCtrl = false;
}
document.onkeydown = function(e) {
if (e.which == 123)
isCtrl = true;
if (((e.which == 85) || (e.which == 65) || (e.which == 88) || (e.which == 67) || (e.which == 86) || (e.which == 2) || (e.which == 3) || (e.which == 123) || (e.which == 83)) && isCtrl == true) {
alert('This is Function Disabled');
return false;
}
}
// right click code
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if (navigator.appName == "Netscape")
document.captureEvents(Event.MOUSEDOWN || Event.MOUSEUP);
function mischandler() {
alert('This is Function Disabled');
return false;
}
function mousehandler(e) {
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if ((eventbutton == 2) || (eventbutton == 3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
//select content code disable alok goyal
function killCopy(e) {
return false
}
function reEnable() {
return true
}
document.onselectstart = new Function("return false")
if (window.sidebar) {
document.onmousedown = killCopy
document.onclick = reEnable
}

It's easy using event.target we can enable/disable right-click when mouse enters/leaves a particular image section on your web page.
Just see a demo of the same below
var isCtrl = false;
document.onkeyup = function(e) {
if (e.which == 17)
isCtrl = false;
}
document.onkeydown = function(e) {
if (e.which == 123)
isCtrl = true;
if (((e.which == 85) || (e.which == 65) || (e.which == 88) ||
(e.which == 67) || (e.which == 86) || (e.which == 2) ||
(e.which == 3) || (e.which == 123) || (e.which == 83)) && isCtrl == true) {
alert('This is Function Disabled');
return false;
}
}
// right click code
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if (navigator.appName == "Netscape")
document.captureEvents(Event.MOUSEDOWN || Event.MOUSEUP);
function mischandler(e) {
let target = $(e.target);
if(!target.is('#img')) {
alert('This is Function Disabled');
return false;
}
}
function mousehandler(e) {
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if ((eventbutton == 2) || (eventbutton == 3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
$('#img').on('mouseenter', function(e) {
mischandler(e);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src='https://www.w3schools.com/html/pic_mountain.jpg' id='img' alt='mountain.jpg' width='200' height='200'/>
Hope, this works fine to you. :) :)

I have updated code which checks if selected element is image. Using event object we can check the nature of selected element
function mischandler(e) { //change
if(e.currentTarget.getAttribute("src")==null) //change
{
alert('This is Function Disabled');
return false;
}
}
function mousehandler(e) {
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if (((eventbutton == 2) || (eventbutton == 3)) && e.currentTarget.getAttribute("src")==null) //change
return false;
}

With jQuery I think a rather simple solution would be:
$(document).on( "contextmenu", function(event) {
//if image allow normal browser behaviou
if(event.target.tagName.toLowerCase() === 'img'){
return true;
}else{
//if any other event target prevent default
event.preventDefault;
alert("no right click");
return false;
}
})

Related

disable enter key on page, but notin textarea

When i press ENTER key the page disappears. So i am disabling the enter key for the form. But If i am in Textarea, I want to enable enter key for new line. NOT A JQUERY SOLUTION PLEASE.
function stopRKey(evt){
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ?
evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {
return false;
}
else if((evt.keyCode==13) && (node.type=="checkbox")){
return false;
}
else if((evt.keyCode==13) && (node.type=="table")){
return false;
}
else if((evt.keyCode==13) && (node.type=="file")){
return false;
}
else if((evt.keyCode==13) && (node.type=="paragraph")){
return false;
}
else if((evt.keyCode==13) && (node.type=="textarea")){
return false;
}
else if((evt.keyCode==13) && (node.type=="list")){
return false;
}
else if((evt.keyCode==13) && (node.type=="choice")){
return false;
}
else if((evt.keyCode==13) && (node.type=="date")){
return false;
}
else if((evt.keyCode==13) && (node.type=="ip_cidr")){
return false;
}
else {
return true;
}
};
document.onkeypress = stopRKey;
document.activeElement gives you the currently focused Element.
function stopRKey(ev){
if (ev.which == 13){ // which normalizes charCode and keyCode
var e = document.activeElement;
if (e.type == "textarea") return true; // this can also be e.id or e.classList
else return false;
}
}
document.onkeypress = stopRKey;
<input type="text">
<textarea></textarea>
Here an another solution:
var textAreas=document.getElementsByTagName('textarea');
for(var i=0;i<textAreas.length;i++){
textAreas[i].onkeypress=stopRKey;
}
I'm on mobile so not tested but this should work
document.addEventListener('keypress', e => {
If(document.querySelector(':focus').type !== 'textarea' && e.keyCode == 13) {
e.preventDefault();
}
});
document.onkeypress = function(evt) {
if(evt.target.toString() !== '[object HTMLTextAreaElement]' && evt.charCode === 13){
evt.preventDefault();
return;
}
};

How to Disable right click both mouse and keyboard in MIcrosoft Edge

Disable right click in Microsoft Edge.This below code works fine in Google and Internet Explorer
document.onmousedown = clickfn;
function clickfn(e) {
var button;
if (navigator.appName == "Microsoft Internet Explorer") {
button = event.button;
}
else {
button = e.buttons;
}
if (button == 2) {
alert("Right Click Disabled");
if (navigator.appName == "Microsoft Internet Explorer") {
event.returnValue = false;
}
return false;
}
}
</script>
This code will disable mouse right-click and keyboard shortcuts as well in IE,edge,chrome,firefox
jQuery(document).ready(function() {
function disableSelection(e) {
if (typeof e.onselectstart != "undefined") e.onselectstart = function() {
return false
};
else if (typeof e.style.MozUserSelect != "undefined") e.style.MozUserSelect = "none";
else e.onmousedown = function() {
return false
};
e.style.cursor = "default"
}
window.onload = function() {
disableSelection(document.body)
};
window.addEventListener("keydown", function(e) {
if (e.ctrlKey && (e.which == 65 || e.which == 66 || e.which == 67 || e.which == 70 || e.which == 73 || e.which == 80 || e.which == 83 || e.which == 85 || e.which == 86)) {
e.preventDefault()
}
});
document.keypress = function(e) {
if (e.ctrlKey && (e.which == 65 || e.which == 66 || e.which == 70 || e.which == 67 || e.which == 73 || e.which == 80 || e.which == 83 || e.which == 85 || e.which == 86)) {}
return false
};
document.onkeydown = function(e) {
e = e || window.event;
if (e.keyCode == 123 || e.keyCode == 18) {
return false
}
};
document.oncontextmenu = function(e) {
var t = e || window.event;
var n = t.target || t.srcElement;
if (n.nodeName != "A") return false
};
document.ondragstart = function() {
return false
};
});

How to Disable source code and Right click mouse? [duplicate]

This question already has answers here:
How do I disable right click on my web page?
(30 answers)
Closed 6 years ago.
Which script is used in this website ? We cannot View Source code (Ctrl+U) and Right click mouse. I want to add like this script to my site.
Could you please provide the script.
var isCtrl = false;
document.onkeyup=function(e)
{
if(e.which == 17)
isCtrl=false;
}
document.onkeydown=function(e)
{
if(e.which == 17)
isCtrl=true;
if((e.which == 85) || (e.which == 67) && (isCtrl == true))
{
return false;
}
}
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
function mischandler(){
return false;
}
function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
That page uses the following code:
document.oncontextmenu = function(e) {
var t = e || window.event;
var n = t.target || t.srcElement;
if (n.nodeName != "A") return false
};
document.ondragstart = function() {
return false
};
function disableSelection(e) {
if (typeof e.onselectstart != "undefined") e.onselectstart = function() {
return false
};
else if (typeof e.style.MozUserSelect != "undefined") e.style.MozUserSelect = "none";
else e.onmousedown = function() {
return false
};
e.style.cursor = "default"
}
window.onload = function() {
disableSelection(document.body)
}
<div style="height: 150; width: 150">
link 1
</div>
By the way, you cannot protect Javascript source code from being stolen or viewed, as the person visiting the page must be able to view the script in order to run it.

Can someone help fix these various JavaScript errors?

This JavaScript code have much errors - any one can help me to make it work?
Line 6: Missing semicolon.
Line 11: Expected ')' to match '(' from line 11 and instead saw ';'.
Line 11: Expected an identifier and instead saw '&'.
Line 11: Expected an assignment or function call and instead saw an
expression.
Line 11: Missing semicolon.
Line 16: Missing semicolon.
Code:
1 <script type='text/javascript'>
2 var isCtrl = false;
3 document.onkeyup=function(e)
4 {
5 if(e.which == 17)
6 isCtrl=false;
7 }
8 document.onkeydown=function(e)
9 {
10 if(e.which == 17)
11 isCtrl=true;
12 if((e.which == 85) || (e.which == 67) && isCtrl == true)
13 {
14 // alert(‘Keyboard shortcuts are cool!’);
15 return false;
16 }
17 }
18 var isNS = (navigator.appName == "Netscape") ? 1 : 0;
19 if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
20 function mischandler(){
21 return false;
22 }
23 function mousehandler(e){
24 var myevent = (isNS) ? e : event;
25 var eventbutton = (isNS) ? myevent.which : myevent.button;
26 if((eventbutton==2)||(eventbutton==3)) return false;
27 }
28 document.oncontextmenu = mischandler;
29 document.onmousedown = mousehandler;
30 document.onmouseup = mousehandler;
31 </script>
You have issue in this line:
if((e.which == 85) || (e.which == 67) && isCtrl == true)
Fixed:
var isCtrl = false;
document.onkeyup=function(e)
{
if(e.which == 17)
isCtrl=false;
}
document.onkeydown=function(e)
{
if(e.which == 17)
isCtrl=true;
if((e.which == 85) || (e.which == 67) && isCtrl == true)
{
return false;
}
}
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
function mischandler(){
return false;
}
function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
<script type='text/javascript'>
var isCtrl = false;
document.onkeyup=function(e)
{
if(e.which == 17)
isCtrl=false;
}
document.onkeydown=function(e)
{
if(e.which == 17) {
isCtrl=true;
}
if((e.which == 85) || (e.which == 67) && isCtrl == true)
{
// alert(‘Keyboard shortcuts are cool!’);
return false;
}
}
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if(navigator.appName == "Netscape") {
document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
}
function mischandler(){
return false;
}
function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
</script>
Errors should be fixed now.
I have changed & ==> & and used {} for single line if statements
P.S. If you want to minify it remove the script tags

Event is not defined error in javascript only Firefox

I use the following script to validate the text box to enter only numbers and (.) which means it is decimal textbox validation. It was work fine in Internet Explorer and Google Chrome. If I execute the function in FireFox I get the following Error:
Event Is not Defined.
How to solve this?
function abc(event) {
if (event.keyCode > 47 && event.keyCode < 58) {
return true;
}
if (event.keyCode == 8 || event.keyCode == 46)
{
return true;
}
return false;
}
I call this function like this:
$('.decimalValidate').live('keypress',function(){
var decimalid=$(this).attr("id");
var decimalval=$('#'+decimalid).val();
var decimalvalidate=abc(decimalval);
if(decimalvalidate == false)
return false;
});
I assign this validation for text box like this:
input type="text" id="Total" class="abc"
Try this
function abc(event) {
if(!event)
event= window.event;
if (event.keyCode > 47 && event.keyCode < 58) {
return true;
}
if (event.keyCode == 8 || event.keyCode == 46)
{
return true;
}
return false;
}
and
$('.decimalValidate').live('keypress',function(e){
var decimalid=$(this).attr("id");
var decimalval=$('#'+decimalid).val();
var decimalvalidate=abc(evt); //keypress event
if(decimalvalidate == false)
return false;
});
decimalval is not an Event object, and you have to pass it to the abc function in ordert to find out which key you pressed:
$('.decimalValidate').live('keypress',function(ev){
var decimalid=$(this).attr("id");
var decimalval=$('#'+decimalid).val();
var decimalvalidate=abc(ev);
if(decimalvalidate == false)
return false;
});
$('.decimalValidate').live('keypress',function(e){
var decimalvalidate=abc(e); //this will point to the event of the keypress.
if(decimalvalidate == false)
return false;
});
I am not sure why you did all of the decimalid and decimalval operations, but if you want the event, do as I wrote in the edited code above.
Good luck.
$('.decimalValidate').on('keypress',function(event){
var decimalid = $(this).attr("id");
var decimalval = $('#'+decimalid).val();
var decimalvalidate = abc(event);
if(decimalvalidate == false)
return false;
});
function abc(event) {
if (event.keyCode > 47 && event.keyCode < 58) {
return true;
}
if (event.keyCode == 8 || event.keyCode == 46)
{
return true;
}
return false;
}
It helps you..

Categories

Resources