Skip to content

Instantly share code, notes, and snippets.

@dpawluk
Created August 21, 2018 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpawluk/7ed4be1e5395868f923ab10c820da883 to your computer and use it in GitHub Desktop.
Save dpawluk/7ed4be1e5395868f923ab10c820da883 to your computer and use it in GitHub Desktop.
This app shows that the window.onbeforeunload() event will fire and allow (some) functions to finish executing before the window is closed.
<html>
<head>
<meta charset="utf-8">
<!-- http://garden.zendesk.com -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zendeskgarden/css-bedrock@7.0" type="text/css">
</head>
<body>
<h2 class="u-gamma">Hello, World!</h2>
<!-- https://github.com/zendesk/zendesk_app_framework_sdk -->
<script type="text/javascript" src="https://assets.zendesk.com/apps/sdk/2.0/zaf_sdk.js"></script>
<script>
// Initialise the Zendesk JavaScript API client
// https://developer.zendesk.com/apps/docs/apps-v2
var client = ZAFClient.init();
client.invoke('resize', { width: '100%', height: '200px' });
client.on('app.registered', init);
function init(context){
console.log(context);
makeTestRequest("app opened request");
window.onbeforeunload = function(){
makeTestRequest("app closed request");
return null;
}
};
function makeTestRequest(keyvalue){
var req = {
url: "https://requestinspector.com/inspect/01cnfdfvgq1mhxq60bckws2bba",
type: "POST",
contentType: "application/json",
data: JSON.stringify({"testkey": keyvalue})
}
client.request(req).then(function(response){
console.log(response);
});
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment