How to load scripts manually for javascript and nodejs?

I am not sure how to load script manually in playcanvas. There there no example to load the script files without the editor.

I am trying to think both ways but not sure how to do in both javascript html and nodejs. To make sure the example work both side in case.

Ideas?

You can load them into an HTML page by tempoarily creating script elements:

head = document.getElementsByTagName('head')[0]
script = document.createElement('script')
script.type = 'text/javascript'
script.src = 'Scripts/'+url+'?' //The oldest javascript file
head.appendChild(script)
head.removeChild(script)

Or you can do an XMLHttpRequest (I use this for json, but you can probably do it for javascript)

function loadJson(url, func){
    //Loads the requested URL and runs the supplied function when loaded
    var req = new XMLHttpRequest();

    req.onreadystatechange = function() {
        if (req.readyState == 4 && req.status == 200) {
            var myArr = JSON.parse(req.responseText);
            
            func(myArr);
        }
    }
    req.open("GET", url, true);
    req.send();
}

Or you can use playcanvas’ built in asset tools (I think they can load javascript). Namely have a look at app.assets.loadFromUrl()

I have no idea what works in nodejs as I haven’t used it, but these are the ways that I know.

I see. Those do not work with the nodejs.

It used require(’./script.js’); or read file as text and then eval() function. That I am missing some or how they load script is different.