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

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.

Related

How to make certain part of an background image clickable

I have a Homepage Takover running on our website. and I have already written code for displaying the image as background image and it is clickable.
But I need certain part of image needs to clickable to another URL.
<!DOCTYPE html>
<html>
<head>
<title>asdfasdf</title>
</head>
<body>
<div>
<script>
if (top == self) {
var interWindow = window;
var interDoc = window.document;
} else {
try {
var interWindow = window.parent;
var interDoc = window.parent.document;
} catch (e) {
/* The creative cannot escape the iframe. Show an appropriate alternative. The alternative will remain inside of the iframe. */
}
}
var timeDelay = 0;
var backgroundColor = '#ffffff';
function initBackground() {
high = window.screen.height;
size = window.screen.width;
if (size <= 1280) {
interDoc.body.style.backgroundImage = none;
} else if (size > 1280 && size < 1440) {
interDoc.body.style.backgroundImage = "url(https://i.ibb.co/L9hnZJ1/hpto.gif)";
} else {
interDoc.body.style.backgroundImage = "url(https://i.ibb.co/L9hnZJ1/hpto.gif)";
}
if (backgroundColor != '') {
interDoc.body.style.backgroundColor = backgroundColor;
}
interDoc.body.style.backgroundRepeat = 'no-repeat';
interDoc.body.style.backgroundPosition = 'top center';
interDoc.body.style.backgroundAttachment = 'fixed';
interDoc.onclick = backGroundClick;
}
var backGroundClick = function (e) {
if (document.all) {
if (event.button == 2 || event.button == 3) {
return false;
}
} else {
if (e.button == 2 || e.button == 3) {
e.preventDefault();
e.stopPropagation();
return false;
}
}
var link = 'google.com';
EE = e ? e : event;
if (!EE) {
return;
}
var t = EE.target ? EE.target : EE.srcElement;
if (t.tagName == "BODY" || t.tagName == "HTML" || t.tagName == "HEADER" || t.tagName == "SECTION" || t.tagName == "FOOTER") {
var ad = window.open("" + link);
} else {
console.log('link click event: ' + t.tagName);
}
}
interDoc.onmouseover = function (e) {
EE = e ? e : event;
if (!EE)
return;
var t = EE.target ? EE.target : EE.srcElement;
if (t.tagName == "BODY" || t.tagName == "HTML" || t.tagName == "HEADER" || t.tagName == "SECTION" || t.tagName == "FOOTER") {
interDoc.body.parentNode.style.cursor = 'pointer';
} else {
interDoc.body.parentNode.style.cursor = 'auto';
}
}
window.setTimeout("initBackground();", timeDelay);
</script>
</div>
</body>
</html>
The Red highlited part in the background image needs to redirect to another URL when onclick.

I want to enable right click on image only

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;
}
})

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;
}
};

Shortening a function to capture CTRL Keyboard events

Just in my humble opinion, the code looks to be long and repetitive, is there a way to perhaps shorten the javascript?
The use of the popular framework jQuery is also permitted:
//-------------------------------------------------------------------->>
// CTRL SHORTCUT FEATURES:
//-------------------------------------------------------------------->>
function KeyDownHandler(evnt) {
var evnt = evnt || window.event;
//CTRL-S
if (evnt.keyCode == 83 && evnt.ctrlKey) {
evnt.preventDefault ? evnt.preventDefault() : event.returnValue = false;
if (document.getElementById('save').disabled == false) {
imts_save_changes()
}
return false;
}
//CTRL-X
else if (evnt.keyCode == 88 && evnt.ctrlKey) {
evnt.preventDefault ? evnt.preventDefault() : event.returnValue = false;
if (document.getElementById('delete').disabled == false) {
imts_delete_record()
}
return false;
}
//CTRL-A
else if (evnt.keyCode == 65 && evnt.ctrlKey) {
evnt.preventDefault ? evnt.preventDefault() : event.returnValue = false;
imts_add_new()
return false;
}
}
I think somebody else can probably do a better job than me, but this is what came to my mind:
function KeyDownHandler(evnt) {
var evnt = evnt || window.event;
if (evnt.ctrlKey) {
evnt.preventDefault ? evnt.preventDefault() : event.returnValue = false;
switch(evnt.keyCode) {
case 83:
if (document.getElementById('save').disabled == false) {
imts_save_changes()
}
break;
case 88:
if (document.getElementById('delete').disabled == false) {
imts_delete_record()
}
break;
case 65:
imts_add_new()
break;
};
}
return false;
}

jquery elastic textarea issue

i am using this plugin :http://www.unwrongest.com/projects/elastic/ and this is my code :
$(document).ready(function () {
$("#<%=Message_txt.ClientID %>").elastic();
var keyPressCount = 0;
$(window).keyup(function (e) {
if (keyPressCount++ % 10 == 0) {
chat.server.onuserTyping(ToClient, fromClient);
}
var enterkey = e.which == 13 || e.KeyCode == 13 || e.charCode == 13 ? true : false;
if (enterkey) {
var Msgtxt = $("#<%=Message_txt.ClientID %>").val().trim();
if (Msgtxt == null) return false;
$("#<%=Send_btn.ClientID %>").trigger("click");
}
});
});
as you can see when the enter key is pressed i am triggering the send_btn click the problem is after the button is triggered the textarea loose the elastic functionality and i don't know why

Categories

Resources