Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll click not working with NavigationManager.NavigateTo, and I can't use href because I need forceLoad functionality

I have a Blazor Server web app; .NET 5.

I am running into frustrations related to navigating between pages in my web app:

  • When I use NavigationManager.NavigateTo(uri, true), I am unable to open links using my scroll click (which would open the link in a new browser tab if I were to use href="uri" instead). It opens a new tab, but loads the same page I was already on.
  • When I use href="uri" instead, the scroll-click works. However, it introduces a new problem: with a 'normal' left-click, the new page loads but retains the scroll position of the previous page in a mobile browser (I have tested with mobile Safari, as well as the mobile emulator in Chrome).

I need to be able to scroll-click into a new tab, as well as have a new page load without retaining the previous page's scroll position. Any tips?

like image 599
sion_corn Avatar asked Jun 30 '21 12:06

sion_corn


1 Answers

Assuming that you are using the onclick event: The onclick event does not listen for the scroll wheel click. To fix this just use the onmousedown event instead:

<button class="btn btn-dark" @onmousedown="OnClick">Test</button>

@code {

    private void OnClick()
    {
        NavigationManager.NavigateTo("/counter", true);
    }

}
like image 180
Philipp Ape Avatar answered Oct 17 '22 22:10

Philipp Ape