JS.addScript reference to file?

The "JavaScript to Quest with ASLEvent" discussion provides a cool way to get Quest and JavaScript to talk to each other. However, the "To test..." section shows a cumbersome way to parse a JavaScript function into a string "s" variable:

s = "<script>"
s = s + "function askAge() {"
s = s + "var answer = prompt('How old are you?');"
s = s + "if (answer != null && answer != '') {"
s = s + "ASLEvent('InputboxCallback', answer);"
s = s + "}}</script>"
JS.addScript(s)
JS.askAge()

Is there a way to put all that into an included JavaScript file? The following didn't work for me:

JS.addScript(GetFileURL("testscript.js"))
JS.askAge ()

The contents of testscript.js are exactly the same as "s" above except for leaving out the closing and opening <script> tags.

Thanks!


That's an odd way to add a script.

I was under the impression that any *.js files included within a Quest game would be automatically loaded at the start. therefore your script would just be:

JS.askAge ()

I can't test this, because I'm limited to using the web editor, which doesn't allow you to add javascript. But if it doesn't work automatically, you could probably do either:

JS.addScript("<script src=\"" + GetFileURL("testscript.js") + "\" />")
JS.askAge ()

or

JS.addScript("<script>" + GetFileData("testscript.js") + "</script>")
JS.askAge ()

or even:

JS.eval(GetFileData("testscript.js"))
JS.askAge ()

(As I can't use external files for my javascript, I'm used to putting them through a JS compression utility that outputs a single line of code, and then just pasting that into a single JS.eval call. I'm pretty sure that's more efficient than having to wrap it i HTML and append it to the document every time)


That did it! These two solutions both work in my desktop editor:

JS.addScript("<script>" + GetFileData("testscript.js") + "</script>")
JS.askAge ()

and:

JS.eval(GetFileData("testscript.js"))
JS.askAge ()

I believe your JS.eval solution (first running the code through minification) works, too.


Yep :)

But if minifying code, make sure to escape it correctly. My usual approach is to to take the output from an online compressor, then run a pair of search/replaces on it; changing \ to \\ and " to ' (or to \" if there's already single quotes in there). Then just stick JS.eval(" on the beginning and ") on the end.

I suspect having it in a separate file is better if you can. I'd only recommend my method if you're on the web editor and so can't upload files to include.


Just curious: why do you only use the web editor?


The desktop one requires Windows.


Just curious: mattgmr1, when you are using the offline editor, why don't you just import the .js file?


I suppose I would if I knew how! I see that Squiffy has an @import command, but that doesn't seem to work in Quest.


In the left tree of the editor you have "Advanced" and there is "Javascript" where you can add script files


Fantastic! Thanks so much.


Log in to post a reply.

Support

Forums