I have a form with URL:
localhost:83/mysite/item/UpdateItems/225
I have a temporary table in which I have loaded data and display in my form from temporary table.
When I refresh the page I don't want to fire an ajax call and truncate the table. But if I click on another link from menu then I do want to fire the ajax call and truncate tables.
Is it possible?
I have used beforeunload
for truncate table when click on another menu:
$(window).on('beforeunload', function(){
//My ajax code for truncate table
});
But this works on page refresh also, which I don't want. So I tried with matching string in URL:
$(window).on('beforeunload', function(e){
if (document.URL.indexOf("UpdateItems") > -1) {
console.log("page refresh");
}else{
console.log("not refreshed");
}
});
But this always go in first page refresh condition even if I have clicked on another menu.
Try this , it will definitely work,
<SCRIPT language="javascript">
window.onbeforeunload=before;
function before(evt)
{
if(window.location.toString().indexOf("UpdateItems") > -1) {
console.log("page refresh");
}
else{
console.log('Not');
}
}
</script>
If (and this is a pretty big if) the refresh is done by clicking a link on the page (and not say from a script or by the user clicking the browsers refresh button) this approach might work:
Set a flag to false whenever someone click a link that should not lead to the table being truncated, and only run the code if the flag is true:
// As default the flag is true.
flagTruncate = true;
// When someone clicks a link with UpdateItems in the URL, the flag is set to false.
$("a[href*='UpdateItems']").click(function () {
flagTruncate = false;
});
//Only truncate the table when the user is leaving if the flag is still true.
$(window).on('beforeunload', function() {
if(flagTruncate) {
//Truncate the table.
}
});
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.