Can you "include" JS from a separate file?

I have a plug-in in development, and would really like to separate the user-editable code into another file. I'm used to PHP where you can simply "include" another file, and it works as if the code is right there. I found some answers for JS, but they all use other libraries (prototype, ajax, etc.): http://stackoverflow.com/questions/21294/dynamically-load-a-javascript-file

Can any of those methods be used here? Is it worth the trouble? My plug-in was really for personal use, but it seems so useful I'd like to release it for everyone else, and this would be a good feature to have. You can view my plug-in code here if that helps: http://forums.rpgmakerweb.com/index.php?/topic/48803-crg-random-text-random-text-when-clicking-on-events/

I know other people use external files and just pull *data* from there (not full code), but I don't know that I have the skills for that.
You have multiple options:


- Make the editable file JSON, instead, load it via AJAX and JSON.parse it (prefered).


- Load the javascript file via AJAX and eval the whole thing (not pretty, but should work).


- Create a new script element and add it to the document. This is also loaded asynchronously, though.


- Make one of the files a Node.js module and require it. Note, that this will only work in local mode.
So it sounds like there's no way to just cut/paste my editable code into a separate file and include it? That's what I was afraid of. Parsing a json file definitely sounds like the best option, I just don't think I have the time/skill to do that. So I might end up just releasing the plug-in as-is. Thanks!
So it sounds like there's no way to just cut/paste my editable code into a separate file and include it? That's what I was afraid of. Parsing a json file definitely sounds like the best option, I just don't think I have the time/skill to do that. So I might end up just releasing the plug-in as-is. Thanks!
if i remember correctly there's a way to use common event comment to place your text there... then read it... using that no need for external files.

if i'm not wrong lavra is the one who made that suggestion in one of the thread in JS learning.
There is, but parsing notetags is more time intensive than JSON.

Parsing a json file definitely sounds like the best option, I just don't think I have the time/skill to do that.
There is no need to do that manually. Do it like this:

var data;var load = function(url) { var xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.overrideMimeType('application/json'); xhr.onload = function() { data = JSON.parse(xhr.responseText); }; xhr.onerror = function() { throw new Error("There was an error loading the file '" + file + "'."); }; xhr.send();};var alias_create = Scene_Boot.prototype.create;Scene_Boot.prototype.create = function() { alias_create.call(this); load(...);};var alias_isReady = Scene_Boot.prototype.isReady;Scene_Boot.prototype.isReady = function() { return data !== undefined && alias_isReady.call(this);};var alias_start = Scene_Boot.prototype.start;Scene_Boot.prototype.start = function() { // some code depending on the loaded data. alias_start.call(this);};This will load the JSON file at the start of the game and wait for it, before executing the rest of your code. Depending on what you want to do, you probably don't even need to alias "start".Now for the parse itself. Assuming your file looks like this:

Code:
{    'key': 4}
This automatically be parsed to a JavaScript object containing the key-value-pair 'key' -> 4. Just remember, that you can only store data in a JSON file. So if you want to read a regex or a function, you need to store their string representation and use new RegExp() / eval() / new Function() to create the object.
You can but its not supported, you'll need babel and to understand either ES6 imports or node require/exports this will allow you to import and export files. I do this for all my plugins. The resulting code is transpiled and not (really) readable, but thats why you also link to the source code 🙂
If i understand the ES6 feature correctly, this is not what the OP wants, since the files will already be linked at compile time and result in a single file, defeating the purpose of having multiple files to begin with (although it makes it easier to maintain an overview over huge plugins during development).
Yes you can, using a pretty standard technique, which involves adding a script tag to the bottom of the rendered page. Here's a script that I dug up for you. Add this somewhere in your JS code.

function loadScript(url, callback){ var script = document.createElement("script") script.type = "text/javascript"; if (script.readyState){ //IE script.onreadystatechange = function(){ if (script.readyState == "loaded" || script.readyState == "complete"){ script.onreadystatechange = null; callback(); } }; } else { //Others script.onload = function(){ callback(); }; } script.src = url; document.getElementsByTagName("head")[0].appendChild(script);}Then to use it, you'll have to use:

loadScript("js/testScript.js", function () { /* do things here when external script loads */ } )You'll quickly notice that this pattern might become a little unwieldy once you are trying to include multiple scripts, due to the way the callback mechanism works. One solution is to find a JS framework (like "require.js") that will handle this pattern for you.
Yes you can, using a pretty standard technique, which involves adding a script tag to the bottom of the rendered page. Here's a script that I dug up for you. Add this somewhere in your JS code.

function loadScript(url, callback){ var script = document.createElement("script") script.type = "text/javascript"; if (script.readyState){ //IE script.onreadystatechange = function(){ if (script.readyState == "loaded" || script.readyState == "complete"){ script.onreadystatechange = null; callback(); } }; } else { //Others script.onload = function(){ callback(); }; } script.src = url; document.getElementsByTagName("head")[0].appendChild(script);}Then to use it, you'll have to use:

loadScript("js/testScript.js", function () { /* do things here when external script loads */ } )You'll quickly notice that this pattern might become a little unwieldy once you are trying to include multiple scripts, due to the way the callback mechanism works. One solution is to find a JS framework (like "require.js") that will handle this pattern for you.

This seems super sketch, I would either use ES6 or nothing at all ... Personally.
This seems super sketch, I would either use ES6 or nothing at all ... Personally.
Welcome to the world of Javascript pre-ES6. These techniques have been used by various frameworks for years.
There is no need to do that manually. Do it like this:
What I meant was that if I were to use a json file, I'd have to rewrite my code. I'm not a great coder and don't know a ton of JS. Basically, this is the main code (it would have lots of CASEs): 

if (command === 'RandomText') { switch (args[0]) { case 'candles': var backgroundCase = '2'; var windowPositionCase = '2'; var showFaceCase = 'true'; var faceNameCase = 'Actor2'; var faceNumberCase = '4'; var object = [ "There are three candles burning brightly.", "A candle holder with three lit candles.", "These candles look like they've been recently lit.", "Let us bask in its warm, glowing, warming glow."]; break;}}-- THEN PICK A RANDOM 'OBJECT' SENTENCE AND DISPLAY THE MESSAGE WINDOW --I'd like all the CASE snippets to be moved to a separate file (those are the user-editable parts), but doing so is probably over my head. Maybe I'll just release it and someone better can update it if they'd like. If it's not as hard as I think it is, maybe I'll give it a shot.

Anyway, thanks for all the suggestions -- I'm sure it'll be useful for other, better coders looking to do the same thing. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/can-you-include-js-from-a-separate-file.53268/