› Forums › Classic DFFS › Undefined for Scripts Added to Custom JS
Tagged: Custom JS, DFFS, external scripts, undefined
- This topic has 3 replies, 2 voices, and was last updated 5 years, 9 months ago by
Alexander Bautz.
-
AuthorPosts
-
-
February 16, 2018 at 02:58 #19874
sgjs2015
ParticipantSo I’ve tried pretty much everything, but I’m not able to access objects added through script files in Custom JS. For example, if I try to add the toastr.js CDN files to load before any Custom JS, it keeps saying that toastr is not defined.
In Chrome, even after the page finishes loading, I can see the script file has been added, but the toastr object cannot be found. This happens to matter what other libraries I try to add (not really specific to toastr).
I’ve attached an image with my sample code. Any thoughts? Have others run into the same issue?
Attachments:
-
February 16, 2018 at 21:46 #19901
Alexander Bautz
KeymasterThe toastr library is rigged for “require.js” compatibility and therefor you will not get a globel varable. As described in the top of the Custom JS textarea you must wrap AMD enabled modules like this:
// AMD module code wrapper - change module_name with the name of the module you are loading require(["toastr"], function(toastr){ toastr.info("Toast example - shows in upper right corner."); });
Alexander
-
February 17, 2018 at 19:00 #19911
sgjs2015
ParticipantIt’s working perfectly with require. Thanks, Alexander!
Additionally, I was also able to get it to work with:
$.getScript("LOCAL_LOCATION_TO_TOASTR_JS", function(){ toastr["info"]("My notification title", "My notification text"); });
However, the require technique seems more robust, so I’ll stick to that.
-
This reply was modified 5 years, 9 months ago by
sgjs2015.
-
This reply was modified 5 years, 9 months ago by
-
February 19, 2018 at 18:54 #19921
Alexander Bautz
KeymasterI’m glad it worked. I’m personally no big fan of require.js (maybe in larger projects, but not in a simple form modification), but had to add support for it because Microsoft added it in SharePoint online.
What happens when you try to use getScript is that (if require.js is loaded) require.js detects it and hijacks the function if it is AMD enabled.
Alexander
-
-
AuthorPosts
- You must be logged in to reply to this topic.