Fiddle - http://liveweave.com/gWoHkb
I been building a starter template/demo for web design applications, and I ran into a problem today.
When I select an anchor I don't want it's href to redirect the browser.
I know I can use javascript:void(0)
or a #
'hash' for this, however I don't want to change it's hash I just don't want it to be a problem when designing.
So I applied return false;
to it via .on(click mousedown touchstart mouseup touchend)
. I noticed that when I did that my drag method for my selected element was also set at return false so it kept dragging even when my mouse was up. (not suppose to happen)
While that solved the redirect problem that also made a bigger problem. So I removed mouseup touchend
from that function. Made a new one to set the following to true.
$('#canvas').children().filter("a").on('click mousedown touchstart', function() {
return false;
}).on('mouseup touchend', function() {
return true;
});
So I decided to go a different route. When the select-tool or edit-tool is active I set all anchor's that have a target="_blank"
property to target="_self"
.
Now the only way I know how to prevent redirection is using unbeforeunload which always prompts with a dialog.
// Ask if they want to leave.
var hook = true;
window.onbeforeunload = function() {
if (hook) {
return "All your stuff may not be saved. Are you sure you want to leave?"
}
}
function unhook() {
hook=false;
}
I don't know of any way to bypass the dialog and stay on the page.
According to @duskwuff you aren't allowed to make a user stay when browser is asked for redirect using onbeforeunload.