João Gomes
João Gomes

Reputation: 340

Detect page change with 2sxc app in Blazor/Oqtane

Since Blazor and Oqtane are SPA, content is added to the DOM with no actual load to be always detected.

Also, 2sxc still does not seem to work with Blazor / .net on the client browser. So no @code{} availale.

This leaves quite a few usual cases like every "on page load" with no options for easy implementation.

So, resorting only to common razor server side code and plain javascript, how can I trigger a js function every time a page is shown, either for the first time or recurrent accesses with no forced reloads?

Let's say a simple:

<script src="sayhello.js"></script>

and

function showalert(){
    alert("This is page ABC");
}
window.onload = showalert();

Upvotes: 1

Views: 46

Answers (1)

Qiang Fu
Qiang Fu

Reputation: 9004

Blazor have page load event, then just use Javascript interop to call javascript function.

@page "/"
@inject IJSRuntime _JS

<script>
    function showalert(){
    alert("This is page ABC");
    }
</script>

@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await _JS.InvokeVoidAsync("showalert");
    }
}

Upvotes: 0

Related Questions