Warn user before leaving web page


I have to warn user before leaving web page with unsaved changes in jquery with custom message in asp.net master page.

I have tried with beforeunload event, but i want show custom message. I have used below code, but only first time alert message is showing second back button click alert is not showing. can someone help on this.

var isChange = false;

$("input").change(function () {
isChange = true;


if (window.history && window.history.pushState) {

$(window).on('popstate', function () {
if (isChange)
alert('You have unsaved data on the page.');
window.history.pushState('forward', null, null);