Avatar of paddycobbett
paddycobbett

asked on 

jQuery: preventing a user to navigate from the current page upon a condition

Please take a look at the code attached, i'm trying to require a confirmation from the user that they want to change the page by applying an onclick on all anchors. Don't worry, it's not one of those nuissance prompts! .. the intention is that it only appears if the user has any unsaved changes. It's probably quite obvious what i'm trying to do from the code. An immediate (without a prompt) return of false from an onclick action causes the anchor to not experience a click (right??), but when i put it in this prompt it seems to navigate anyway.. the prompt appears momentarily whilst the pages begins to relocate. Can anyone shed some light on what i'm missing here to get it working?
$("a").click(function(){   						
	   if (modified){
		jConfirm("<font size='3'>You have unsaved content changes. Are you sure you want to navigate to a different page?</font>", "Prompt", function(r) {
			if (r){
				return true;
			}else{
				return false;
			}
	});											
	}
});

Open in new window

jQueryJavaScript

Avatar of undefined
Last Comment
paddycobbett

8/22/2022 - Mon