B4J Question [BANano]: How can I AddJavaScript & AddCSSFile based on demand?

Mashiane

Expert
Licensed User
Longtime User
I've tried to work on some script but no matter what I seem to try, this does not cut it. Is this something that is impossible perhaps / not recommended?

This is based on the fact that the last file to add is app.js on the scripts list and need to add whatever needed files before that.

B4X:
'inject javascript method on demand
Sub InjectJavaScriptFile(jsURL As String)
    Dim sFile As String = $"./scripts/${jsURL}"$
    BANano.RunJavascriptMethod("loadjscssfile",Array As String(sFile,"js"))
End Sub

'inject javascript method on demand
Sub InjectCSSFile(jsURL As String)
    Dim sFile As String = $"./styles/${jsURL}"$
    BANano.RunJavascriptMethod("loadjscssfile",Array As String(sFile,"css"))
End Sub

#if javascript
function loadjscssfile(filename, filetype){
    if (filetype=="js"){
        var fileref=document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src", filename);
    }
    else if (filetype=="css"){
        var fileref=document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", filename);
    }
    if (typeof fileref!="undefined"){
        var head = document.getElementsByTagName('head')[0];
        var lastChild = head.lastChild;
        head.insertBefore(fileref, lastChild);
    }
}
#End If
 
Upvote 0
Top