Refresh Page and Goto Page Bottom

In a menu item, I use the following javascript to create a button to refresh a page :

javascript:location.reload(true)

However, I could not find the right javascript codes for both refresh and goto a page bottom after the refresh (or goto the previous scroll position).

Could anyone help?

Alex

Hi @AlagoLowe,
Assuming you have a button inside a rich text view looking like:
<button id="refreshAndScroll">Refresh>/button>

You can use the below piece of code in a scene render event handler, which re-renders the views in the page, waits a little bit, and then scrolls to previous position (I’ve included an optional ‘scroll to bottom’ in a commented piece of code).

You will need to replace scene_XX with the scene_key that your button is on.

You may need to adjust the delay of the timeout to suit.

I’m curious to hear what your use case is for this?
Let me know how that goes!

$(document).on('knack-scene-render.scene_XX', function(event, view, record) {

  $("#refreshAndScroll").click(function () {

    var scrollPosition = $(window).scrollTop();

    Knack.router.scene_view.render();
    setTimeout(function() {
      // Use this function to scroll to bottom of page instead: $(document).scrollTop($(document).height());
      $(window).scrollTop(scrollPosition);
    }, 2000); // Adjust the delay as needed
  
  });

});
2 Likes

Hi Stephen,

Thank you for your kind help.
Because this javascript will be used in a menu item (in the URL field), it has to be a one line instruction.
Also, with the latest Knack’s performance, using setTimeout() may not be a good choice.

Alex