0

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.

Daniel Jackson
  • 1,036
  • 1
  • 11
  • 35
  • Use AJAX. With jQuery, all you need to do is `$("#container").get("page.html");` –  Apr 06 '17 at 20:31
  • @ChrisG Slightly more involved than that. You'd be repeating the `` and everything inside your `#container` over and over again. – mpen Apr 06 '17 at 20:51
  • @mpen I assumed it's obvious that page.html only contains content, not the entire document. It was just an example line, not a complete solution. –  Apr 06 '17 at 22:09
  • Something like that may have some success in it. I will see about working with this. Thanks for the comments everyone. – Daniel Jackson Apr 07 '17 at 17:41
  • I guess the only real thing I'm confused on is what do I do with `$('#container-fluid').get('page.html')` ? I'm alerting this and am getting back undefined. This shows how newbish I am lol. – Daniel Jackson Apr 07 '17 at 19:19

1 Answers1

1

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

Community
  • 1
  • 1
  • The idea is good, but the solution isn't thought well through. Assume the page is refreshed like this twice - then it looks like this: `https://www.example.com?page_y=364?page_y=532`. As you can see, it doesn't work the second time. It also doesn't work if the URL already contains other GET parameters. And if the URL is `https://www.example.com/no_page_yet`, it doesn't work either. – Aloso Apr 06 '17 at 20:55
  • @Aloso I didn't think of that idea, follow the link to the original post –  Apr 07 '17 at 07:25
  • I did find a really awesome solution but I cannot post it because somehow I am banned from posting answers when I've never posted an answer before on this site. – Daniel Jackson Apr 11 '17 at 19:23