spfjs
spfjs copied to clipboard
Reload current page and keep scroll position
Is there some way to reload the current page from JavaScript while keeping the current scroll position?
This is what I do now:
spf.navigate(window.location.href);
but this doesn't retain the scroll position.
There isn't any code to do exactly what you're looking for yet. However, if your scroll position won't change during page updates (meaning your refreshed content takes roughly the same same height), and if you're not using chunked json responses (what's called "multipart" in SPF), then you can probably use spf.load
instead of spf.navigate
as a work-around for now:
spf.load(window.location.href);
spf.load
reloads the page, but somehow all the JavaScript listeners/bindings on the elements in the page are gone when the page has reloaded. It doesn't trigger any of the SPF events (spfrequest, spfprocess, spfdone) so I don't see how to rebind the JS listeners...
Am I doing something wrong here, or is there some way to rebind everything?
edit:
I was using the spfdone
event to execute JavaScript that has to run when the page has loaded, I guess I should move that to the foot
section of the SPF response? What is the best way to do this?
Does spf.load
trigger any SPF events?
Disregard the previous message. I moved the initialization JavaScript from the spfdone
event to the foot
section of the SPF response and now both spf.load
and spf.navigate
are working.
Glad to hear that it's working.
spf.load
does not trigger any events since it's meant to be used for more traditional in-page updates (e.g. "load more", etc) and therefore has no limit on the number of simultaneous requests (see http://youtube.github.io/spfjs/api/#spf.load)
To handle the life cycle of spf.load
, use the callback-based API described at http://youtube.github.io/spfjs/documentation/events/#callbacks-and-cancellations. If you have code like:
function handleDone(evt) { initializePage(evt.detail.response); }
window.addEventListener('spfdone', handleDone);
you can re-use it with:
// The argument to the callback will be an object that conforms to the
// spf.EventDetail interface for "spfdone" events.
function doneCallback(detail) { initializePage(detail.response); }
spf.load(url, {onDone: doneCallback});