onunload does not complete
-
Saturday, February 11, 2012 11:16 AM
Hi everyone,
I have a problem when trying to perform a number of actions when a gadget is closed (by clicking the X). I handle the onunload event and I can track that a number of steps are executed but not all of the code in the onunload function finish. I make a couple of jQuery .ajax calls to a webservice (that logs some info). I know the syntax of the function is OK as the same code is executed during the normal operation of the gadget.
Is there any reason that an unload function might not complete?
Thanks,
Mark.
All Replies
-
Saturday, February 11, 2012 3:15 PM
Probably if it has to wait for the tiniest little thing I'd imagine it would fail...hard to know without seeing the script and after that - for me - it might even be harder still.
Been ages since I played with it since I always had problems too but now I can't stop it from working - made the example below which writes two files (I tried as many as ten) to the documents folder then queries the color of an element and copies it to the clipboard.
I'm glad you came along !
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>test Unload</title> <style type="text/css"> body {margin:0;width:130px;} </style> <script language="javascript" type="text/javascript"> function pageLoad() { prs.innerText="loaded"; } function pageUnload() { var myDocs=System.Shell.knownFolderPath('Documents'), fso=new ActiveXObject("Scripting.FileSystemObject"), sFile; sFile=fso.CreateTextFile(myDocs+"\\onunloadtest.txt", true); sFile.WriteLine("Testing"); sFile.WriteLine("Testing2Testing"); sFile.WriteLine("Testing3Testing3Testing"); sFile.WriteLine("Testing4Testing4Testing4Testing"); sFile.WriteLine("Testing5Testing5Testing5Testing5Testing"); sFile.WriteLine("Testing6Testing6Testing6Testing6Testing"); sFile.WriteLine("Testing7Testing7Testing7Testing"); sFile.WriteLine("Testing8Testing8Testing"); sFile.WriteLine("Testing9Testing"); sFile.WriteLine("Testing10"); sFile.Close(); sFile=fso.CreateTextFile(myDocs+"\\onunloadtest2.txt", true); sFile.WriteLine("Test"); sFile.WriteLine("Test2Test"); sFile.WriteLine("Test3Test3Test"); sFile.WriteLine("Test4Test4Test4Test"); sFile.WriteLine("Test5Test5Test5Test5Test"); sFile.WriteLine("Test6Test6Test6Test6Test"); sFile.WriteLine("Test7Test7Test7Test"); sFile.WriteLine("Test8Test8Test"); sFile.WriteLine("Test9Test"); sFile.WriteLine("Test10"); sFile.Close(); fso=null; window.clipboardData.setData('Text',prs.style.color); } </script> </head> <body> <div id="prs" style="width:130px;color:#f00">Hi</div> <script language="javascript" type="text/javascript"> window.onload=pageLoad; window.onunload=pageUnload; </script> </body> </html>
-
Sunday, February 12, 2012 6:47 AM
Hi mystifeid thanks for the reply.
I took your writing to a file idea and added something similar to the end of my onunload handler. To my surprise the code ran (well something output to the file), however my ajax request was still not received by the server. Using fiddler I can see that the request was never sent. I soon realised what was happening though, as I make a number of other ajax requests (using jQuery.ajax, not that I think that matters specifically though) for logging/debug purposes some of which complete, I was probably getting to the point where too many requests were being made (from memory I think IE only allows 2 concurrent requests). I bet that I hit this limit and the gadget closed before the 'close-down' ajax request was due to be processed.
When I turned off the logging requests the close-down ajax request was sent and received by the server. The way I have 'fixed' this is to make all the requests after onunload is fired synchronous, that way they should all be required to complete (well sent at least) before the gadget is closed. Seems to be working fine.
Thanks for helping out.
Regards,
Mark
-
Sunday, February 12, 2012 9:00 AMVery good to know - thanks for sharing.
-
Tuesday, February 21, 2012 2:00 AM
It seems that you can also use onunload to save all current gadget settings. But I think you must test for the existence of a currently saved setting in Settings.ini. This can be one of your own or there is just one that is created by and always present in both Vista and Windows 7 - "PrivateSetting_GadgetName".
If you don't do this you end up with lots of duplicated settings in Settings.ini that belong to no one gadget.
eg say you have
window.onunload=saveMySettings;
then it would be very bad to use this function as the event handler
function saveMySettings(){
System.Gadget.Settings.writeString("gadgetState","fubar");
}
because sooner or later it will be fubarred all right.
However from testing so far, the following seems to work ok on both Vista and Win 7
function saveMySettings(){
if(System.Gadget.Settings.readString("PrivateSetting_GadgetName")!==""){
System.Gadget.Settings.writeString("gadgetState","fubarNoMore");
}
}
ie if you log off or reboot, it will save the settings but if you close the gadget by clicking on the "x" it will not save the settings.Edit - A week later, I finally notice this does not seem to be working with any shutdown function on my Vista PC


