Daniel Jackson
Daniel Jackson

Reputation: 1126

Refresh a site page just like pressing F5 or clicking refresh button

I have looked around and don't see anything about what I'm asking, only turning this off...

So I'm using Microsoft Edge and when I run my ASP.Net Core web app, there's changes that I want to show. I can press F5 or click the refresh button and the page doesn't flash or it doesn't look like it reloads except for the data showing up. Or if I scroll down the page, hit the refresh button where the page then does flash, it returns back to the spot that I was scrolled to before hitting refresh button. How can I make this happen in code?

In JavaScript I can force a page refresh with location.reload() except this seems to do a complete "hard" refresh and does not resume back to the position I was scrolled down to. So how can F5 do this but I can't? I assume there must be a way.

I've searched and searched but everyone wants to turn this off not on. Any help is much appreciated.

Upvotes: 0

Views: 1369

Answers (1)

user6152171
user6152171

Reputation:

Use this code to refresh the page:

//code to refresh the page
var page_y = $( document ).scrollTop();
window.location.href = window.location.href + '?page_y=' + page_y;

And place this code in a script tag at the bottom of body:

//code to handle setting page offset on load
$(function() {
    if ( window.location.href.indexOf( 'page_y' ) != -1 ) {
        //gets the number from end of url
        var match = window.location.href.split('?')[1].match( /\d+$/ );
        var page_y = match[0];

        //sets the page offset 
        $( 'html, body' ).scrollTop( page_y );
    }
});

Code from: https://stackoverflow.com/a/17643341/6152171

Upvotes: 1

Related Questions