$(document).on('mousedown', 'a', function(event){
event.preventDefault();
if(event.which == 1){
if($(this).attr('target') != '_blank'){
loadpage($(this).attr('href'));
}
}
}).on('contextmenu', 'a', function(event){
event.preventDefault();
});
Hello once again Stackoverflow!
For my current project I want to disable the right and middle mouse button on every link. And when clicked on with the left mouse button, if the link doesn't contain target="_blank", I need to call a function that loads that page using AJAX. (function loadpage()).
This piece of code works decently, although the middle mouse button still opens a new tab. How do I solve this?
Thanks in advance!
Within that event handler, call
e.preventDefault():
$("#foo").on('click', function(e) {
if( e.which == 2 ) {
e.preventDefault();
}
});
or:
Disable mouse wheel event by using JAVASCRIPT :
In IE:
document.attachEvent('onmousewheel', function(e){
if (!e) var e = window.event;
e.returnValue = false;
e.cancelBubble = true;
return false;
}, false);
In Safari:
document.addEventListener('mousewheel', function(e){
e.stopPropagation();
e.preventDefault();
e.cancelBubble = false;
return false;
}, false);
In Opera:
document.attachEvent('mousewheel', function(e){
if (!e) var e = window.event;
e.returnValue = false;
e.cancelBubble = true;
return false;
}, false);
In Firefox:
document.addEventListener('DOMMouseScroll', function(e){
e.stopPropagation();
e.preventDefault();
e.cancelBubble = false;
return false;
}, false);
Related
I need to use onbeforeunload everywhere, when user leave my site.
var leave_message = 'Leave message';
var validNavigation;
function goodbye(e) {
if (!validNavigation) {
if (!e) e = window.event;
e.cancelBubble = true;
e.returnValue = leave_message;
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
return leave_message;
}
}
$("body").on('click', 'a', function () {
validNavigation = true;
});
window.onbeforeunload = goodbye;
But there is many places in my javascript, where i use window.location for redirecting user to internal pages. And i tired of pasting validNavigation = true; in these parts of code.
Please give me advice how i can setup it globally?
My Actual Task is to confirm the user when he closes the tab or window.
For that I wrote the following code,
var validNavigation = false;
function wireUpEvents() {
var dont_confirm_leave = 0;
var leave_message = 'You sure you want to leave?'
function goodbye(e) {
if (!validNavigation) {
if (dont_confirm_leave!==1) {
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
return leave_message;
}
}
}
window.onbeforeunload=goodbye;
// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function(e) {
if (e.keyCode == 116){
validNavigation = true;
}
});
// Attach the event click for all links in the page
$("a").bind("click", function() {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type='button']").bind("click", function() {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
wireUpEvents();
});
the problem here is it asks confirmation if the user reloads the web page.
So i need to bind the reload event.
Please help me...
Thanks in advance
Use .unload or .on('unload', handler):
$(window).unload(function() {
// your code here
});
I'm Trying to disable Ctrl++ / Ctrl+- browsers shortcuts via javascript :
$(document).ready(function(){
$(document).keydown(function(event) {
if (event.ctrlKey==true && (event.which == '107' || event.which == '109')) {
alert('disabling zooming ! ');
event.preventDefault();
}
});
});
This code is working great in FF and Chrome , and dosent prevent zooming in IE ! any idea ?
This works for me, though you may want to bind to 'keyup' also.
$(document).ready(function () {
$(document).bind('keydown keypress', function (event) {
event.preventDefault();
});
});
There are more than 2 button numbers you have to preventDefault in order to disable fully the scrolling.
Me personally I disable all ctrl key combinations.
$(document).ready(function () {
function preventDefault(e) {
e = e || window.event;
if (e.preventDefault)
e.preventDefault();
e.returnValue = false;
}
$(document).bind('keydown keypress', function (event) {
if (event.ctrlKey) {
preventDefault(event);
return false;
}
});
});
I am trying to get this code working on drupal, but only works when I press f5, a links or forms are not working, any idea what is the problem with this code?
My objective is to kill the session when you close the tab or close the explorer. This code is a modification from this link: http://eureka.ykyuen.info/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/#comment-6936
<code>
(function($) {
var validNavigation = false;
var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
var leave_message = 'Do you want to exit?';
function goodbye(e) {
if (!validNavigation) {
if (dont_confirm_leave!==1) {
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
// window.open('DelLogged.php?Id=');
return leave_message;
}
}
}
// Attach the event click for all links in the page
$("a").click(function () {
validNavigation = true;
alert("Link press");
});
// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function(e) {
if (e.keyCode == 116){
validNavigation = true;
alert("F5 press");
}
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function() {
validNavigation = true;
alert("Form press");
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function() {
validNavigation = true;
alert("input press");
});
window.onbeforeunload=goodbye;
})(jQuery);
</code>
I think you're not using the (function($){}(jQuery)); closure correctly, as the brackets are not written the right way. You've written
(function($) {
...
})(jQuery);
closing the initial bracket before (jQuery);
Try changing this and lets see if may solve the issue :)
I'm trying to find a way of disabling the default action of the mouse wheel button which is to open the link in a new tab.
Is that possible?
Bind a generic click event handler that specifically checks for middle clicks. Within that event handler, call e.preventDefault():
$("#foo").on('click', function(e) {
if( e.which == 2 ) {
e.preventDefault();
}
});
Note that not all browsers support preventing this default action. For me, it only works in Chrome. Firefox, Opera and IE9 all do not raise the click event with middle mouse click. They do raise mouseup and mousedown.
This works for me...
$(document).on("mousedown", "selector", function (ev) {
if (ev.which == 2) {
ev.preventDefault();
alert("middle button");
return false;
}
});
My code:
$(document).on('auxclick', 'a', function(e) {
if (e.which === 2) { //middle Click
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
return false;
}
return true;
Disable mouse wheel event by using JAVASCRIPT :
In IE:
document.attachEvent('onmousewheel', function(e){
if (!e) var e = window.event;
e.returnValue = false;
e.cancelBubble = true;
return false;
}, false);
In Safari:
document.addEventListener('mousewheel', function(e){
e.stopPropagation();
e.preventDefault();
e.cancelBubble = false;
return false;
}, false);
In Opera:
document.attachEvent('mousewheel', function(e){
if (!e) var e = window.event;
e.returnValue = false;
e.cancelBubble = true;
return false;
}, false);
In Firefox:
document.addEventListener('DOMMouseScroll', function(e){
e.stopPropagation();
e.preventDefault();
e.cancelBubble = false;
return false;
}, false);