Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Include js files to the web app created by Qt



  • I would like to call the js libraries by EM_JS, I try to manually include the scripts in the html file(app_name.html ) after compiled. Like

    app_name.html

    <scirpt src="js/config.js"><script>
    

    js/config.js

    window._config =  {
    
    api : {
    
    invokeUrl: 'abcd"
    
    }
    
    }
    

    mainwindow.cpp

    ``'
    EM_JS(char*, get_invoke_url, (), {
    var jsString = window._config.api.invokeUrl;
    var lengthBytes = lengthBytesUTF8(jsString)+1;
    var stringOnWasmHeap = _malloc(lengthBytes);
    stringToUTF8(jsString, stringOnWasmHeap, lengthBytes);
    return stringOnWasmHeap;
    })

    void process_input_text::on_pushButtonSelectCharsInFile_clicked()
    {
    char *url = get_invoke_url();
    ui->lineEditCharsInFile->setText(url);
    free(url);
    }

    
    After I call the on_pushButtonSelectCharsInFile_clicked, it give me error message 
    
    **TypError : window._config is undefined**
    
    How should I include the js file in order to call it from c++?Thanks


  • @tham said in Include js files to the web app created by Qt:

    <scirpt src="js/config.js"><script>

    Try spelling script correctly?



  • @jonb Thanks, tried it, but still the same.

    I include the script like

    <script src='config.js'></script>
    <script type='text/javascript'>
    //codes generated by Qt
    </script>
    

    different part is I change window to Module
    Now it complain Module._config is undefined



  • Wonder if it is caused by name conflict etc, so I change it to

    var _global_config =  {
    
    api : {
    
    invokeUrl: 'abcd"
    
    }
    
    }
    

    call it in EM_JS as

    _global_config.api.invokeUrl
    

    Ok, at last it works. Maybe window._config and Module._config works too, maybe I was forgot to update the html or wrong spelling etc, I am not sure, sometimes this kind of small errors are hard to spot, hope someday we could have compile time error for them.


Log in to reply