Open
Description
Thank you @MackinnonBuck for providing this sample project - I integrated it into Oqtane (https://github.com/oqtane/oqtane.framework) so that we can provide developers with a much easier method for integrating Bootstrap templates.
A few notes for other developers who may be interested in utilizing this BlazorPageScript component:
- It relies on "JavaScript Initializers" (https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/startup?view=aspnetcore-8.0) which allow you to execute logic before and after a Blazor app loads.
- The JavaScript Initializer is named wwwroot/BlazorPageScript.lib.module.js - it registers a custom HTML element called "page-script" which accepts a single parameter of "src".
- The most critical event when running on Static Blazor using Enhanced Navigation is the "onUpdate" event. This event is called on every page transition and basically replaces the traditional role of the JavaScript "onload" event.
- The onUpdate event is not just called once - it is called multiple times. Originally I thought it would be a good idea to create a function so that it only executes my custom script once (ie. by comparing the url with the previous url) however this turned out to be a bad idea as the onUpdate event can be triggered prior to all components on your page being fully rendered - which means that if your script relies on the existence of a specific element - that element may not exist on the page yet. So its best to process every onUpdate event.
- When you create your script you usually only need to implement the onUpdate event as it handles new page loads as well as page transitions.
function myPageInitializer() {
// implementation
}
export function onUpdate() {
myPageInitializer();
}
- If the script you want to execute contains window.addEventListener('load', xxx) methods you will want to remove them as the "load" event is never called in page transitions.
window.addEventListener('load', () => {
if (window.location.hash) {
if (select(window.location.hash)) {
scrollto(window.location.hash)
}
}
});
becomes
if (window.location.hash) {
if (select(window.location.hash)) {
scrollto(window.location.hash)
}
}
Metadata
Metadata
Assignees
Labels
No labels