How to Inject your Custom JavaScript in Modern Pages on Page Load

This SPFx functionality helps to inject your custom JavaScript file in your modern site pages on page load.

[code lang=”c”]
export default class MySPFXApplicationCustomizer
extends BaseApplicationCustomizer<IMySPFXApplicationCustomizerProperties> {

private _customJsUrl: string = “https://sharepointdomain.net/scripts/MyCustonScript.js”;

@override
public onInit(): Promise<void> {

let scriptTag: HTMLScriptElement = document.createElement(“script”);
scriptTag.src = this._customJsUrl;
scriptTag.type = “text/javascript”;
document.getElementsByTagName(“head”)[0].appendChild(scriptTag);

return Promise.resolve<void>();
}
}
[/code]

This solution is brought to you by our SharePoint professionals.

Softree Consulting employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *