diff --git a/data/contest.com.html b/data/contest.com.html
index 27ad67299e..8770251129 100644
--- a/data/contest.com.html
+++ b/data/contest.com.html
@@ -18,7 +18,7 @@
     <link href="/css/bootstrap.min.css?3" rel="stylesheet">
     <link href="/css/bootstrap-extra.css?2" rel="stylesheet">
     <link href="/css/telegram.css?212" rel="stylesheet">
-    <link href="/css/contest.css?38" rel="stylesheet">
+    <link href="/css/contest.css?39" rel="stylesheet">
 <link href="/css/contest-zoo.css?7" rel="stylesheet">
 <link href="/css/jquery-ui.min.css" rel="stylesheet">
 
@@ -182,7 +182,7 @@
 <script src="/js/jquery-ui.min.js?1"></script>
 <script src="/js/tgsticker.js?24"></script>
 
-    <script>ajInit({"version":366,"apiUrl":"\/api?hash=telegram-crawler","unauth":true});</script>
+    <script>ajInit({"version":367,"apiUrl":"\/api?hash=telegram-crawler","unauth":true});</script>
     <script id="aj_script">Aj._useScrollHack=true;
 Aj.onLoad(function(state) {
   function requestConfirmation(event) {
diff --git a/data/themes.telegram.org/js/main-aj.js b/data/themes.telegram.org/js/main-aj.js
index 5eb48bd8ee..8c0cdd64d7 100644
--- a/data/themes.telegram.org/js/main-aj.js
+++ b/data/themes.telegram.org/js/main-aj.js
@@ -246,7 +246,6 @@ function ajInit(options) {
   }
 
   function pageLoaded() {
-    curBeforeUnload = false;
     if (curOnLoad.length) {
       for (var i = 0; i < curOnLoad.length; i++) {
         console.log('onLoad', i);
@@ -263,7 +262,6 @@ function ajInit(options) {
   }
 
   function layerLoaded() {
-    curBeforeLayerUnload = false;
     if (curOnLayerLoad.length) {
       for (var i = 0; i < curOnLayerLoad.length; i++) {
         console.log('onLayerLoad', i);
@@ -415,7 +413,7 @@ function ajInit(options) {
       if (result.j) {
         window.execScript ? window.execScript(result.j) : eval(result.j);
       }
-      Aj.layerLoaded();
+      layerLoaded();
       return;
     }
     return changeLocation(url, push_state);
@@ -563,12 +561,17 @@ function ajInit(options) {
     if (!message && curBeforeUnload) {
       message = curBeforeUnload();
     }
+    var load_func = function() {
+      curBeforeLayerUnload = false;
+      curBeforeUnload = false;
+      load_fn();
+    };
     if (message) {
       var message_html = $('<div>').text(message).html();
-      showConfirm(message_html, load_fn, l('WEB_LEAVE_PAGE', 'Leave'));
+      showConfirm(message_html, load_func, l('WEB_LEAVE_PAGE', 'Leave'));
       return false;
     } else {
-      load_fn();
+      load_func();
       return true;
     }
   }
diff --git a/data/tsf.telegram.org/js/main-aj.js b/data/tsf.telegram.org/js/main-aj.js
index 5eb48bd8ee..8c0cdd64d7 100644
--- a/data/tsf.telegram.org/js/main-aj.js
+++ b/data/tsf.telegram.org/js/main-aj.js
@@ -246,7 +246,6 @@ function ajInit(options) {
   }
 
   function pageLoaded() {
-    curBeforeUnload = false;
     if (curOnLoad.length) {
       for (var i = 0; i < curOnLoad.length; i++) {
         console.log('onLoad', i);
@@ -263,7 +262,6 @@ function ajInit(options) {
   }
 
   function layerLoaded() {
-    curBeforeLayerUnload = false;
     if (curOnLayerLoad.length) {
       for (var i = 0; i < curOnLayerLoad.length; i++) {
         console.log('onLayerLoad', i);
@@ -415,7 +413,7 @@ function ajInit(options) {
       if (result.j) {
         window.execScript ? window.execScript(result.j) : eval(result.j);
       }
-      Aj.layerLoaded();
+      layerLoaded();
       return;
     }
     return changeLocation(url, push_state);
@@ -563,12 +561,17 @@ function ajInit(options) {
     if (!message && curBeforeUnload) {
       message = curBeforeUnload();
     }
+    var load_func = function() {
+      curBeforeLayerUnload = false;
+      curBeforeUnload = false;
+      load_fn();
+    };
     if (message) {
       var message_html = $('<div>').text(message).html();
-      showConfirm(message_html, load_fn, l('WEB_LEAVE_PAGE', 'Leave'));
+      showConfirm(message_html, load_func, l('WEB_LEAVE_PAGE', 'Leave'));
       return false;
     } else {
-      load_fn();
+      load_func();
       return true;
     }
   }