When going back a page, scroll to the last known vertical position

This is only needed for JS-enhanced links. Without javascript to mess it up,
the browser handles this 100% correctly by itself.
This commit is contained in:
Jason McBrayer 2018-09-17 20:12:53 -04:00
parent 203bd5070c
commit 075b8f67fe
2 changed files with 23 additions and 1 deletions

View File

@ -145,3 +145,22 @@ function characterCount()
{
return $("#id_status").val().length + $("#id_spoiler_text").val().length;
}
function savePlace()
{
sessionStorage.setItem("lastScrollPos", window.scrollY);
return true;
}
function restorePlace(ev)
{
var lastScrollPos = sessionStorage.getItem('lastScrollPos');
Intercooler.ready(function (elt)
{
if (elt[0].tagName == 'BODY')
{
window.scrollTo(0, lastScrollPos);
}
});
return false;
}

View File

@ -40,7 +40,8 @@
{% endif %}
</head>
<body class="has-navbar-fixed-top" ic-history-elt="true"
ic-global-include='{"csrfmiddlewaretoken": "{{ csrf_token }}"}'>
ic-global-include='{"csrfmiddlewaretoken": "{{ csrf_token }}"}'
ic-on-beforeSend="savePlace()">
<div id="page-load-indicator">&nbsp</div>
{% block navbar %}
<nav class="navbar is-fixed-top" role="navigation"
@ -199,6 +200,8 @@
<script type="application/javascript">
document.addEventListener('DOMContentLoaded', menuPrepare);
$(document).on('handle.onpopstate.ic',
restorePlace);
</script>
{% block page_scripts_inline %}
{% endblock %}