Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 7025

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 2162

Deprecated: Hook imagify_allow_picture_tags_for_webp is deprecated since version 2.2! Use imagify_allow_picture_tags_for_nextgen instead. in /www/collab365_296/public/wp-includes/functions.php on line 5758
JavaScript Injection Wilth Multiple JS File References - Collab365
Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Warning: Undefined array key "url" in /www/collab365_296/public/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/smart-site/classes/class-tvd-smart-shortcodes.php on line 85

2015-04-07

Sometimes as a developer, the most elegant solution is the one that works. Recently, I was faced with the challenge of attempting to get JavaScript code to run consistently on certain pages in a SharePoint Online site.

Certain aspects of the project dictated that JavaScript Injection via a SharePoint hosted app was the only alternative to add the code to the site. I started with the Core.EmbedJavaScriptJSOM project from the Office365 Developers Patterns and Practices site on GitHub. This works really well if you are referencing a single JS reference file such as the jQuery library; however, I was unable to figure out how to get it to work with multiple file references, which was crucial in my case.

I won’t bore you with all of methods that I tried that didn’t work and will just say that after several fairly frustrating hours of trial and error, the pattern below is what eventually did work. I’m still using the core of the Core.EmbedJavaScriptJSOM app and have just modified the RemoteManager_Inject function in the Content/scenario1.js file ( file which is attached) to handle making sure all of the necessary JS files were loaded before my code was executed. Also, many thanks to David Husdon and his jquery-getscripts solution which I have leveraged, as well.

Although my solution works, it doesn’t seem to pass the elegance eye-test and I welcome any comments on things that may have been missed or can be improved.

Here is the file that contains the above code:

(Visited 138 times, 1 visits today)

About the author 

Rob Aycock

Summit Bundle

Get 200+ hours of Microsoft 365 Training for 27$!

Master Office 365, Power Platform & SharePoint & Teams With 200+ Hours Of Training Videos and 108 Ebooks in the Collab365 Academy. This offer is insane and is only available for a limited period.