Injecting javascript into SharePoint online communication site

Occasional Contributor

Hi All
I need to inject some javascript into all sharepoint online site collection pages. I managed to do it in Team site by adding javascript file link in to a master page, but cannot find the way to do it in communication site. I found master page in communication site and managed to add my javascript file link to it, but it seems to only work on system pages (sharepoint site settings etc) . I did see some articles where its done by creating SPFX sharepoint app and importing it into the site, but doing all that just to add few lines of javascript (comparing to just adding a link into master page) seems crazy .

3 Replies

@RomanRomych Depending on what you are trying to do, it's likely not supported. Modern Pages do not support the same DOM injection that we used to use in classic. To run JavaScript on your modern pages, you would indeed have to build an SPFx extension. 

However, if you build an SPFx extension that is used to change out of the box components such as web parts, sections, navigation, etc... this would be unsupported, so be careful doing so.

@Beau Cameron Thanks

 So we cannot really add much custom scripts into modern sharepoint? :sad:

@RomanRomych Correct. As mentioned, you can use SPFx Application Customizers, but you shouldn't inject JavaScript to customize out of the box components. It's only supported to inject code into pre-defined placeholders on the page (top and bottom).