简体   繁体   中英

Leaving page javascript/jquery

My question is a continuation of this post https://stackoverflow.com/questions/22438863/javascript-jquery-for-before-leaving-the-page?noredirect=1#comment34138272_22438863 , it's a new function.

Am looking for below function:

In stackoverflow, if you type some answers and if you try to close that tab it will ask for confirmation message..

Below is my Html part:

<form id="demo">
 <input type="text" id="name" name=""/>
 <input type="email" id="emails" name=""/>
 <input type="number" id="ph" name=""/>
 <select name="" id="sample">
  <option>Select</option>  
  <option value="1">Chennai</option>
  <option value="2">Hyderabad</option>
 </select>
</form>

If user enters something and he clicks on logo to navigate away it asks for confirm message..its natural right???Even stackoerflow has this function..I tried this code but didn't worked

var changesMade = false;

function onDataChanged() {
 changesMade = true;
}

$('input:text, textarea, select').change(onDataChanged);
$('input:checkbox, input:radio').click(onDataChanged);

$("#homeicon").click(function() {
 if (changesMade) {
    return 'Changes have been made. Are you sure you want to leave the page?';
    location.href = "home.html";
 } else {
    return null;
 }
});

You are looking for onbeforeunload or beforeunload in jQuery.

check here as well as googling for other places with that term.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM