From 75869a561f2f378956d80e12c51f3611cddd4284 Mon Sep 17 00:00:00 2001 From: Robert Lord Date: Mon, 21 Apr 2014 11:31:32 -0400 Subject: [PATCH] Prevent reload page on language change, fixes #36 - Uses onpopstate to handle users pressing back buttons - Switches the query string and changes the language without reloading. - Thanks to @bootstraponline for some of the code in this commit. --- source/javascripts/app/lang.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/source/javascripts/app/lang.js b/source/javascripts/app/lang.js index 1d0685f..9c7fe91 100644 --- a/source/javascripts/app/lang.js +++ b/source/javascripts/app/lang.js @@ -55,4 +55,21 @@ under the License. }); } + + // if we click on a language tab, activate that language + $(function() { + $("#lang-selector a").on("click", function() { + var lang = $(this).data("language-name"); + var hash = window.location.hash; + if (hash) hash = hash.replace(/^#+/, ''); + // do not reload the page every time the language is changed + if (history) history.pushState({}, '', '?' + lang + '#' + hash); + + activateLanguage(lang); + return false; + }); + window.onpopstate = function(event) { + activateLanguage(window.location.search.substr(1)); + }; + }); })(window);