From 8bb9e1c47555408ecd45705e33cf3a6bbe2469c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Ant=C3=BAnez?= Date: Sun, 22 Jan 2017 08:53:46 +0100 Subject: [PATCH] Back and forward buttons on browser Wonderful work by Ramon https://github.com/ramon-src --- static/js/webslides-lite.js | 39 ++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/static/js/webslides-lite.js b/static/js/webslides-lite.js index 02bcb7f..d13bce2 100644 --- a/static/js/webslides-lite.js +++ b/static/js/webslides-lite.js @@ -311,8 +311,45 @@ jQuery(document).ready(function($){ } } }); + + /** + * Bind the event HashChange when the prev/next history button was clicked + */ + jQuery(window).bind("hashchange", function () { + if (hasHash()) { + goToSlideIfSlideHashChange(); + } else { + window.location.reload(); + } + }); + function hasHash() { + return window.location.hash ? true : false; + } + function goToSlideIfSlideHashChange() { + var paramsArr = getArrayOfHashParams(); + var slideObj = $.grep(paramsArr, function (e) { + return (e.key == "slide"); + }); + if (slideObj.length == 1) { + goToSlide(slideObj[0].value); + } + } + + function getArrayOfHashParams() { + var hash = window.location.hash.replace('#', '').split('&'); + var paramsArr = new Array(); + for (var i = 0; i < hash.length; i++) { + var itemArray = hash[i].split('='); + var action = new Object(); + action.key = itemArray[0]; + action.value = itemArray[1]; + paramsArr.push(action); + } + return paramsArr; + } + // Tabs jQuery('ul.tabs li').click(function(){ var $this = jQuery(this); @@ -331,4 +368,4 @@ jQuery(document).ready(function($){ $('body').toggleClass('baseline').css('height', $(document).height()); } }); - \ No newline at end of file +