javascript - stop redirecting page if it is redirecting by clicking on anchor -


i want prevent page redirecting know can achieve this

window.onbeforeunload = function() {         return "dude, sure want leave? think of kittens!"; } 

which answered here prevent form of page refresh using jquery/javascript

but in case want prevent page when redirecting clicking on of anchor tag.

also event.preventdefault() not work in case while clicking on anchor tag because anchors not redirecting page should work fine.

i want stop redirecting page if redirecting clicking on anchor. solution?

you can keep flag tells whether or not user clicked a tag, read on onbeforeunload script:

window.onbeforeunload = function () {     if (_clickedanchor) {         _clickedanchor = false;         return "dude, sure want leave? think of kittens!";     } }  _clickedanchor = false;  jquery(document).ready(function () {     jquery("a").click(function () {         _clickedanchor = true;     }); }); 

Comments