I implemented this on my app but this code is related to the certain page(Form) but how handle closing page event for other pages(forms)?
Not sure if I probably understand your issue but you may be looking for the form hide event
Hi @Ilqar - I moved this to a new topic since the previous topic was over 3 years old.
Could you outline what your use case is, what you’ve tried, and what’s not working.
Note the previous post was written before anvil.js
was added, and it’s probably much simpler to do what you want now with anvil.js
.
But it’s tricky to help without knowing the specifics.
Thank you Stu,
I have application with many Forms
On only one Form I need raise event when Form is closing - trigger some Function on that Form which is making anvil.call for Server Function which delete saved to database information.
When I working on that Form and close application it is asking - “Do you want to close page?” and no matter is what you answer code is running and everything is OK
but if I moved to the other pages and close application I get error message
I tried to implement different HTML to my Form which rating event but again getting error when close application when closing from other Forms
<div class="leaving"></div>
<script>
window.onbeforeunload = function(event) {
event.returnValue = "Write something clever here..";
add_status();
};
function add_status(){
var firstArgument = $('.leaving');
anvil.call(firstArgument, "Delete_Proposal", "leaving").then(function (r) {
console.log("The function returned:", r);
});
e.preventDefault();}
</script>
on my FORM I put this client code:
def Delete_Proposal(self):
if self.drop_down_chap_Et.selected !=[] and self.drop_down_Hansi_Xidmet_uzre.selected !=[] and self.drop_down_Teklifin_tarixi.selected !=[]:
Mush_Adi = self.drop_down_chap_Et.selected[0]
Hansi_Xidmet_uzre = self.drop_down_Hansi_Xidmet_uzre.selected[0]
Teklifin_Tarixi = self.drop_down_Teklifin_tarixi.selected[0]
anvil.server.call('Delete_PDF',Mush_Adi,Hansi_Xidmet_uzre,Teklifin_Tarixi)
else:
pass
Stu I just solved this:
I put on Form’s HTML with event code with Function but on other Forms’ HTML code without Function
I guess it’s worth remembering you can do this all in python now
from anvil.js import window
class MyForm(MyFormTemplate):
...
def handle_unload(self, e):
if ...: # delete_proposal condition
e.returnValue = ""
e.preventDefault()
# rest of the logic for delete_proposal
# hook up form_show and form_hide events
def form_show(self, **event_args):
window.onbeforeunload = self.handle_unload
def form_hide(self, **event_args):
window.onbeforeunload = None