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

Discussion in 'B4J Questions' started by Mashiane, Jan 7, 2019.

  1. Mashiane

    Mashiane Expert Licensed User

    Hi there

    Depending on what component I might need on my app, I'd like to AddJavaScriptFile & AddCSSFile on demand. For example, in a particular page I need a FlowChart and only want to load the flowchart as and when needed?

    Is this possible or will it be possible in future versions?

    Thank you.
    joulongleu likes this.
  2. Mashiane

    Mashiane Expert Licensed 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.

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

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

    #if javascript
    function loadjscssfile(filename, filetype){
        if (filetype=="js"){
            var fileref=document.createElement('script');
            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
    Rubsanpe and joulongleu like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice