The best way to add JS file to your SharePoint Online site is to use custom actions. Now, if you need to turn it on and off and need a page from where to be able to do so, keep reading below -
Throw the html code provided below on any page using content editor webpart after first updating the link to the js file you want to add and it would do the trick -
Before adding custom action
After adding custom action
HTML Code -
<div>
<div id="details">
<h2>Registered custom actions</h2>
</div>
<button id="addBOT" style="display:none" type="button">Add BOT User Action</button>
<button id="deleteBOT" style="display:none" type="button">Delete BOT User Action</button>
<script type="text/javascript">
$(document).ready(function ()
{
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems);
});
function retrieveListItems() {
var clientContext = new SP.ClientContext.get_current();
this.userCustomActions = clientContext.get_site().get_userCustomActions();
clientContext.load(this.userCustomActions);
clientContext.executeQueryAsync(Function.createDelegate(this, listCustomActions), Function.createDelegate(this, onQueryFailed));
console.log('Libraries Loaded');
}
function listCustomActions(){
var customActionEnumerator = this.userCustomActions.getEnumerator();
var BOTadded = false;
while (customActionEnumerator.moveNext())
{
var oUserCustomAction = customActionEnumerator.get_current();
if(oUserCustomAction.get_title()=="BOTAction"){BOTadded = true;}
$("#details").append("<p>"+ oUserCustomAction.get_title() +"</p>");
}
if(BOTadded){
$("#deleteBOT").show();
}else{
$("#addBOT").show();
}
$("#addBOT").click(function(){
var clientContext = new SP.ClientContext.get_current();
var userActions = clientContext.get_site().get_userCustomActions();
var newUserAction = userActions.add();
newUserAction.set_name("BOTAction");
newUserAction.set_title("BOTAction");
newUserAction.set_location("ScriptLink");
newUserAction.set_scriptSrc("<<LINK TO YOUR JS FILE LOCATION OR CDN>>");
newUserAction.set_sequence(20000);
newUserAction.update();
clientContext.load(userActions);
clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
});
$("#deleteBOT").click(function(){
this.clientContext = new SP.ClientContext.get_current();
this.userActions = this.clientContext.get_site().get_userCustomActions();
this.clientContext.load(this.userActions);
this.clientContext.executeQueryAsync(Function.createDelegate(this, deleteCustomActions), Function.createDelegate(this, onQueryFailed));
});
};
function deleteCustomActions(){
var customActionEnumerator = this.userActions.getEnumerator();
while (customActionEnumerator.moveNext())
{
var oUserCustomAction = customActionEnumerator.get_current();
if(oUserCustomAction.get_title()=="BOTAction"){
oUserCustomAction.deleteObject();
this.clientContext.load(oUserCustomAction);
this.clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
}
}
}
function onQuerySucceeded() {
console.log('Action Completed');
document.location.reload(true)
}
function onQueryFailed(sender, args) {
console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>