Can someone help fix these various JavaScript errors? - javascript

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

Related

Can't enter numbers using numbers pad

I have a function that formats user input into phone format.
It works fine except it's not allowing numbers from numbers pad at the right, Only the numbers at the top of the alphabetic characters.
I want to keep the same format, But allow entering numbers from numbers pad.
Here is a fiddle:
https://jsfiddle.net/s1wyrmk6
Here is the code:
HTML:
<input type="text" id="phone">
JS/jQuery:
(function ($) {
$.fn.usPhoneFormat = function (options) {
var params = $.extend({
format: 'xxx-xxx-xxxx',
international: false,
}, options);
if (params.format === 'xxx-xxx-xxxx') {
$(this).bind('paste', function (e) {
e.preventDefault();
var inputValue = e.originalEvent.clipboardData.getData('Text');
if (!$.isNumeric(inputValue)) {
return false;
} else {
inputValue = String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3"));
$(this).val(inputValue);
$(this).val('');
inputValue = inputValue.substring(0, 12);
$(this).val(inputValue);
}
});
$(this).on('keydown touchend', function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
var curchr = this.value.length;
var curval = $(this).val();
if (curchr == 3 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
} else if (curchr == 7 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
}
$(this).attr('maxlength', '12');
});
} else if (params.format === '(xxx) xxx-xxxx') {
$(this).on('keydown touchend', function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
var curchr = this.value.length;
var curval = $(this).val();
if (curchr == 3 && e.which != 8 && e.which != 0) {
$(this).val('(' + curval + ')' + " ");
} else if (curchr == 9 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
}
$(this).attr('maxlength', '14');
});
$(this).bind('paste', function (e) {
e.preventDefault();
var inputValue = e.originalEvent.clipboardData.getData('Text');
if (!$.isNumeric(inputValue)) {
return false;
} else {
inputValue = String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3"));
$(this).val(inputValue);
$(this).val('');
inputValue = inputValue.substring(0, 14);
$(this).val(inputValue);
}
});
}
}
}(jQuery));
Change the following
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) { return false; }
The additional range 96 to 105 is greater than 57 currently, which causes this statement to catch the numpad.
To allow the numpad:
if (e.which != 8 && e.which != 0 && (e.which < 48 || (e.which > 57 && !(e.which>=96 && e.which<=105 )))) {
return false;
}

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

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.

JavaScript keydown error in IE

<script>
var personX = 18;
var personY = 13;
function processArrowKeys(E) {
if (E.keyCode == 37 || E.keyCode == 38 || E.keyCode == 39 || E.keyCode==40) {
E.preventDefault();
}
if (E.keyCode == 37) {
if (currentterrain[personX - 1][personY] == 0 || currentterrain[personX - 1][personY] == 1 || currentterrain[personX - 1][personY] == 3) {
personX--;
}
}
if (E.keyCode == 39) {
if (currentterrain[personX + 1][personY] == 0 || currentterrain[personX + 1][personY] == 1 || currentterrain[personX + 1][personY] == 3) {
personX++;
}
}
if (E.keyCode == 38) {
for (i = 0; i < 3; i++) {
if (currentterrain[personX][personY - 1] == 0 || currentterrain[personX][personY - 1] == 1 || currentterrain[personX][personY - 1] == 3) {
personY--;
}
}
}
}
</script>
<body onkeydown="processArrowKeys(event)">
The IE debugger says that it expects an object and brakes on "handleArrowKeys(event)".
This works in FF and Chrome
I don't know why this fails, but it does.
changing this line fixed it:
if(E.keyCode==37||E.keyCode==38||E.keyCode==39||E.keyCode==40){if(navigator.appName!="Microsoft Internet Explorer"){E.preventDefault();}}
IE must not work with preventDefault()
Try the following:
/* ... */
function processArrowKeys(E) {
if (!E) E = window.event;
/* ... */
This should work out:
onload = function() {
var body = document.body,
personX = 18,
personY = 13;
body.onkeydown = function( E ) {
E = E || window.event;
if (E.keyCode == 37 || E.keyCode == 38 || E.keyCode == 39 || E.keyCode==40) {
if ( E.preventDefault ) {
E.preventDefault();
} else {
E.returnValue = false;
}
}
if (E.keyCode == 37) {
if (currentterrain[personX - 1][personY] == 0 || currentterrain[personX - 1][personY] == 1 || currentterrain[personX - 1][personY] == 3) {
personX--;
}
}
if (E.keyCode == 39) {
if (currentterrain[personX + 1][personY] == 0 || currentterrain[personX + 1][personY] == 1 || currentterrain[personX + 1][personY] == 3) {
personX++;
}
}
if (E.keyCode == 38) {
for (i = 0; i < 3; i++) {
if (currentterrain[personX][personY - 1] == 0 || currentterrain[personX][personY - 1] == 1 || currentterrain[personX][personY - 1] == 3) {
personY--;
}
}
}
}
}
Change
<body onkeydown="handleArrowKeys(event)">
To
<body onkeydown="processArrowKeys(event)">
Not sure how it works in firefox and chrome since there is no handleArrowKeys function.

Categories

Resources