External Script Files problem

Hi There,

I've been following this topic to try and get global javascript working:
https://community.plumsail.com/t/is-it-possible-to-store-plumsail-javascript-functions-in-an-external-file/10100

I'm using Sharepoint forms and in my test I'm overriding the EditForm.aspx and opening the form full page by browsing to editform.aspx?id=# directly.

I've got the following code in my form:

fd.created(function(){
var imported = document.createElement('script');
imported.src = '/sites/Testing/Sources/Scripts/globals.js';
document.head.appendChild(imported);
});

and my test script contains a simple function:

function sayHello()
{
alert("hello!");
}

but I'm getting the error:

ReferenceError: sayHello is not defined
at eval (eval at e._executeCustomJavaScript (spform.js:116:105460), :20:5)
at spform.js:45:23858
at Array.map ()
at Function.e.safeRun (spform.js:45:23836)
at t. (spform.js:126:34213)
at spform.js:126:25498
at Object.next (spform.js:126:25603)
at a (spform.js:126:24343)

After a lot of experimentation I wonder if it's some kind of caching that is going on with SharePoint. Using the code editor it seems like newer versions of scripts aren't always updated and even when your force update with CTRL+F5 sometimes the old script is still loaded again when you revisit the page. Either way I don't think this is an issue on plumsail's side. thanks

Further to this. If you want to store external JS files in the a Sharepoint document library then be aware that the file will be cached and changes won't be immediately reflected.

I've found a workaround by adding a query string param to the script reference e.g.

fd.created(function(){
var imported = document.createElement('script');
imported.src = "/sites/Testing/Sources/Scripts/lxit-globals.js?v=5";
document.head.appendChild(imported);
});

If you increment the v=5 to 6,7,8 when you make changes then it invalidates the cache and pulls through changes.

1 Like