Injecting javascript into SharePoint online communication site

%3CLINGO-SUB%20id%3D%22lingo-sub-1958631%22%20slang%3D%22en-US%22%3EInjecting%20javascript%20into%20SharePoint%20online%20communication%20site%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1958631%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%3CBR%20%2F%3EI%20need%20to%20inject%20some%20javascript%20into%20all%20sharepoint%20online%20site%20collection%20pages.%20I%20managed%20to%20do%20it%20in%20Team%20site%20by%20adding%20javascript%20file%20link%20in%20to%20a%20master%20page%2C%20but%20cannot%20find%20the%20way%20to%20do%20it%20in%20communication%20site.%20I%20found%20master%20page%20in%20communication%20site%20and%20managed%20to%20add%20my%20javascript%20file%20link%20to%20it%2C%20but%20it%20seems%20to%20only%20work%20on%20system%20pages%20(sharepoint%20site%20settings%20etc)%20.%20I%20did%20see%20some%20articles%20where%20its%20done%20by%20creating%20SPFX%20sharepoint%20app%20and%20importing%20it%20into%20the%20site%2C%20but%20doing%20all%20that%20just%20to%20add%20few%20lines%20of%20javascript%20(comparing%20to%20just%20adding%20a%20link%20into%20master%20page)%20seems%20crazy%20.%3CBR%20%2F%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1958631%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1962732%22%20slang%3D%22en-US%22%3ERe%3A%20Injecting%20javascript%20into%20SharePoint%20online%20communication%20site%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1962732%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F797190%22%20target%3D%22_blank%22%3E%40RomanRomych%3C%2FA%3E%26nbsp%3BDepending%20on%20what%20you%20are%20trying%20to%20do%2C%20it's%20likely%20not%20supported.%20Modern%20Pages%20do%20not%20support%20the%20same%20DOM%20injection%20that%20we%20used%20to%20use%20in%20classic.%20To%20run%20JavaScript%20on%20your%20modern%20pages%2C%20you%20would%20indeed%20have%20to%20build%20an%20SPFx%20extension.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3EHowever%2C%20if%20you%20build%20an%20SPFx%20extension%20that%20is%20used%20to%20change%20out%20of%20the%20box%20components%20such%20as%20web%20parts%2C%20sections%2C%20navigation%2C%20etc...%20this%20would%20be%20unsupported%2C%20so%20be%20careful%20doing%20so.%3C%2FP%3E%3C%2FLINGO-BODY%3E
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 .
Thanks

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).