this script disables left click without any reason - javascript

I have this script:
window.onload = function(){
document.getElementById('workspace').oncontextmenu = function(){
return false;
}
}
function click(e) {
if (navigator.appName == 'Netscape' && e.which == 3) {
window.location.reload();
return false;
else {
if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2)
window.location.reload();
return false;
}
return true;
}
document.onmousedown=click;
The workspace is id of my <body> element. Basically this script does is, it reloads the current page when right clicked on the page. I'm happy with it, this is all I want.
But this script is having a bug. The problem is the left click is disabled. I have some text on webpage but the you can select the text with this script enabled.
I have no much experience with JavaScript but I tried removing the last line of the script. When I did so the left click started working and I can select the text, but soon I noted that reload does not works.

It could be because of click function return false in the third line.
you need } before else { and need to wrap with { } for last if block.
something like this should be ok.
function click(e) {
if (navigator.appName == 'Netscape' && e.which == 3) {
window.location.reload()
return false;
} else {
if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2) {
window.location.reload();
return false;
}
}
return true;
}

Related

How to disable View source and inspect element

How do you disable/ view source/ and /inspect element/, ctrl + u ctrl+shift+I f12 menu bar and right click, also ctrl + s ctrl p ctrl+v ctrl+a ctrl+c and drag select page, please answer all parts that's possible, I prefer to do this will JavaScript array keycodes or html no php or other languages.also I want to block ifram use on my site like somesites such as google.
As I understand it is not possible to completely disable view source and inspect element, so I want minification of code and rest of my question answered instead.
Edit:
I solved alot of it myself, I used onkeydown return false to disable all keys, still need the arrays, I disabled inspect element menu bar by forcing browser to window.open I still need right click, however would like to add that I need a custom right click menu, I disabled the possibility to disable Javascript in order to stop the key block by using noscript function redirects. I also still need the drag and select part. I would still like betterways to fix it...maybe even just minify the code or encrypt it. Of anyone needs some of the code I used just reply. I just need to fix it.
It is not possible to prevent the user from inspecting code running on their machine. At the end of the day the HTMl they are getting delivered will be readable in plain text. You can cause a nuisance for most people, but this will not be a valid security measure - chrome extensions will still run, for instance, so if someone is using the NoScript extension it will disable all javascript.
A much better option would be to handle your logic serverside, and only send the client the information they need to know/requested.
There are some free javascript obfuscators, such as https://javascriptobfuscator.com/. Please remember that it is not a secure method, though.
I mean no matter how much you block it a person can just type
view-source:https://example.com
document.onkeydown = function(e)
{
if(event.keyCode == 123)
{
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0))
{
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0))
{
return false;
}
if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0))
{
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0))
{
return false;
}
}
e is a keyboard event. e.[key] returnes true if key pressed.
If document.onkeydown returns false, key doesn't count.
This programm seeing if code view combination pressed and returning false.
Example. if ctrl, shift and 'J' pressed - return false.
Bump
To the people saying it isn't possible, how would you recon this website managed to do so?
The following website disabled, view source, right click and the dev console.
I am genuinely interested.
https://www.techgyd.com/contact-facebook-directly/6579/
Edit:
all input from keyboard is disabled, but by adding "view-source:" before the httpps:// to the url to become:
view-source:https://www.techgyd.com/contact-facebook-directly/6579/
makes me able to see.
If you would like to know how they did that then take a look at their JS, raw copy/paste:
<script type="text/javascript">
//<![CDATA[
var show_msg = '';
if (show_msg !== '0') {
var options = {view_src: "View Source is disabled!", inspect_elem: "Inspect Element is disabled!", right_click: "Right click is disabled!", copy_cut_paste_content: "Cut/Copy/Paste is disabled!", image_drop: "Image Drag-n-Drop is disabled!" }
} else {
var options = '';
}
function nocontextmenu(e) { return false; }
document.oncontextmenu = nocontextmenu;
document.ondragstart = function() { return false;}
document.onmousedown = function (event) {
event = (event || window.event);
if (event.keyCode === 123) {
if (show_msg !== '0') {show_toast('inspect_elem');}
return false;
}
}
document.onkeydown = function (event) {
event = (event || window.event);
//alert(event.keyCode); return false;
if (event.keyCode === 123 ||
event.ctrlKey && event.shiftKey && event.keyCode === 73 ||
event.ctrlKey && event.shiftKey && event.keyCode === 75) {
if (show_msg !== '0') {show_toast('inspect_elem');}
return false;
}
if (event.ctrlKey && event.keyCode === 85) {
if (show_msg !== '0') {show_toast('view_src');}
return false;
}
}
function addMultiEventListener(element, eventNames, listener) {
var events = eventNames.split(' ');
for (var i = 0, iLen = events.length; i < iLen; i++) {
element.addEventListener(events[i], function (e) {
e.preventDefault();
if (show_msg !== '0') {
show_toast(listener);
}
});
}
}
addMultiEventListener(document, 'contextmenu', 'right_click');
addMultiEventListener(document, 'cut copy paste print', 'copy_cut_paste_content');
addMultiEventListener(document, 'drag drop', 'image_drop');
function show_toast(text) {
var x = document.getElementById("amm_drcfw_toast_msg");
x.innerHTML = eval('options.' + text);
x.className = "show";
setTimeout(function () {
x.className = x.className.replace("show", "")
}, 3000);
}
//]]>
</script>
or just look from line 86
I hope it helps

Internet Explorer stay on the current page after clicking cancel button

On Internet Explorer, i am using below code
if (navigator.appName == 'Microsoft Internet Explorer' || !!(navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/rv:11/)) || (typeof $.browser !== "undefined" && $.browser.msie == 1)) {
window.onbeforeunload = function () {
//return "Ticket upload progress will be lost if you close/reload this window.\nDo you really want to continue?"
var message = confirm("Ticket upload progress will be lost if you close/reload this window.\nDo you really want to continue?");
if (message == true) {
alert(1);
}
else if(message == false) {
return;
}
}
}
When user click on ok or cancel in both the conditions, page is closed.
I want to stay on same page, if user clicked on cancel.
I have tried everything but not working.
Any help is highly appreciated.
Try to replace
else if(message == false) {
return;
}
with
else if(message === false) {
return null;
}

js fullscreen toggle button goes to fullscreen but won't exit it

I have this function tied to an onclick event of a button. It should check to see if the documentElement it should toggle full screen mode and swap the button image.
function toggleFS() {
var fsmode = (document.fullScreenElement && document.fullScreenElement !== null) || // alternative standard method
(document.mozFullScreen || document.webkitIsFullScreen);
var page = document.documentElement;
if(!fsmode) {
if(page.requestFullscreen) {
page.requestFullscreen();
} else if (page.mozRequestFullScreen) {
page.mozRequestFullScreen();
} else if (page.webkitRequestFullScreen) {
page.webkitRequestFullScreen();
}
document.getElementById("toggle-fs").innerHTML = '<img src="/images/nofs.png">';
} else {
if (page.exitFullscreen) {
page.exitFullscreen();
} else if (page.msExitFullscreen) {
page.msExitFullscreen();
} else if (page.mozCancelFullScreen) {
page.mozCancelFullScreen();
} else if (page.webkitExitFullscreen) {
page.webkitExitFullscreen();
}
document.getElementById("toggle-fs").innerHTML = '<img src="/images/fs.png">';
}
}
On the first click after page load, it works correctly and puts the page in fullscreen and switches the button to the exit fullscreen image.
On the second click, it replaces the image for the button but does not exit fullscreen. (Hitting 'ESC' still works.)
Any following clicks do nothing at all. So it is stuck in fullscreen with the go to fullscreen button.
This behavior is in Chrome 56.
Can anyone see where I've gone wrong here?
The functions to request full screen, such as webkitRequestFullScreen, are on document.documentElement, but the ones to exit full screen, such as webkitExitFullscreen, are just on document. The snippet below works properly on Chrome, Edge, and IE.
document.getElementById("toggle-fs").addEventListener("click", function() {
toggleFS()
});
function isFullScreen() {
return (document.fullScreenElement && document.fullScreenElement !== null) ||
(document.msFullscreenElement && document.msFullscreenElement !== null) ||
(document.mozFullScreen || document.webkitIsFullScreen);
}
function enterFS() {
var page = document.documentElement
if (page.requestFullscreen) page.requestFullscreen();
else if (page.mozRequestFullScreen) page.mozRequestFullScreen();
else if (page.msRequestFullscreen) page.msRequestFullscreen();
else if (page.webkitRequestFullScreen) page.webkitRequestFullScreen();
}
function exitFS() {
if (document.exitFullScreen) return document.exitFullScreen();
else if (document.webkitExitFullscreen) return document.webkitExitFullscreen();
else if (document.msExitFullscreen) return document.msExitFullscreen();
else if (document.mozCancelFullScreen) return document.mozCancelFullScreen();
}
function toggleFS() {
if (!isFullScreen()) {
enterFS();
document.getElementById("toggle-fs").innerHTML = '<img src="/images/nofs.png">';
} else {
exitFS();
document.getElementById("toggle-fs").innerHTML = '<img src="/images/fs.png">';
}
}
JsFiddle
Try this one
<button id="toggle-fs" onclick="toggleFullScreen()"><img src="/images/nofs.png"></button>
with...
document.getElementById("toggle-fs").style.display = "block";
Good luck!

Trap keypress using jquery

I have written a code block to trap key press on Ctrl + sit works and an alert has been placed when the event has ran to show me that the code block has successfully ran. later this code block will be replaced by a save command.
However I am having some trouble as the first time Ctrl + s is pressed it works however every time afterwards the s button alone triggers the event
here is the code block:
var isCtrl = false;
ck.on('contentDom', function (evt) {
ck.document.on('keyup', function (event) {
if (event.data.$.keyCode === 17) isCtrl = false;
});
ck.document.on('keydown', function (event) {
if (event.data.$.keyCode === 17) isCtrl = true;
if (event.data.$.keyCode === 83 && isCtrl === true) {
//The preventDefault() call prevents the browser's save popup to appear.
//The try statement fixes a weird IE error.
try {
event.data.$.preventDefault();
} catch (err) { }
alert('ctrl-s');
return false;
}
});
}, ck.element.$);
}
any help is greatly appreciated.
This is a way to perform ctrl+s command in jquery that works every time:
$(window).keypress(function(event) {
if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true;
alert("Ctrl-S pressed");
event.preventDefault();
return false;
});
This post is for anyone who has also had trouble with this problem, I have now solved it. The problem with my original code block is that once isCtrl was set as true it remained true. which meant that when time came again to check if it was pressed it didn't matter whether it was pressed again as it was already set to true. there for I have added a line to change it back to false when the conditional statements have ran.
Here is the new code block:
var isCtrl = false;
ck.on('contentDom', function (evt) {
ck.document.on('keyup', function (event) {
if (event.data.$.keyCode === 17) isCtrl = false;
});
ck.document.on('keydown', function (event) {
if (event.data.$.keyCode == 17) isCtrl = true;
if (event.data.$.keyCode == 83 && isCtrl === true) {
//The preventDefault() call prevents the browser's save popup to appear.
//The try statement fixes a weird IE error.
try {
event.data.$.preventDefault();
} catch (err) { }
alert('ctrl-s');
isCtrl = false;
return false;
}
});
}, ck.element.$);
}

mouse : disable right and middle button click

I've used this but it doesn't work; no errors in the firebug:
$("div").live('mousedown', function(e) {
if( (e.which == 1) ) {
return ;
}if( (e.which == 3) ) {
return false;
}else if( (e.which == 2) ) {
return false;
}
})
I can disable right click with contextmenu but I dont know what to do about middle button.
You can do so with Javascript and/or an HTML attribute (which is really a Javascript event handler anyway) as described here
<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
function disableclick(e)
{
if (e.button == 2) {
alert(status);
return false;
}
}
</script>
and
<body oncontextmenu="return false">
...
</body>
That being said: DON'T DO IT.
Why? Because it achieves nothing other than annoying users. Also many browsers have a security option to disallow disabling of the right click (context) menu anyway.
Not sure why you'd want to. If it's out of some misplaced belief that you can protect your source code or images that way, think again: you can't.
You missed an 'else' in your code.
$("div").live('mousedown', function(e) {
if( (e.which == 1) ) {
return ;
}else if( (e.which == 3) ) {
return false;
}else if( (e.which == 2) ) {
return false;
}
})

Categories