Importing a Javascript file at run time

mlapl1
mlapl1 Community Member Posts: 350 ♪ Opening Act ♪
Hello

Normally an included JS file is included in a title's html file when the title is compiled (I place it in Header Scripting). It is therefore hard-coded into the title. I would like to avoid this hard-coding.

I would like to be able to include the file at runtime so that the content might be changed in the JS file without the need to re-compile the title. How can I do this? I tried to place <script src="./extern/myscripts.js"></script> in the header scripting section but that did not work - maybe I got the path wrong or maybe..... I may also need to add various bits and pieces to the original .js file to tell the system it is a javascript file.

Any clues would be gratefully received.

Andrew

Comments

  • klaatu
    klaatu Community Member Posts: 986 ☆ Roadie ☆
    Put it in bottom of file scripting. Although this is totally wrong from a web design standpoint, because modern  browsers are very good at working with broken HTML it will work.
  • mlapl1
    mlapl1 Community Member Posts: 350 ♪ Opening Act ♪
    OK. I will do that.

    Can I leave the file in extern or do I need to reference it from the root pf the title?

    Also I presume I do not need to add <script></script> tags in the file itself as the tags are already part of the include.

    Thanks yet again Darrel!
  • klaatu
    klaatu Community Member Posts: 986 ☆ Roadie ☆
    You need to include the script tags with the source attribute. I believe you should be able to keep the external JS file anywhere as long as it is on the same server for security reasons.

    The attached example shows that you can replace a lectora variable with a variable from external JS. This is good because you do not have to re-publish to change the variable.

    I've asked this before. If a Trivantis application developer see this I would like to know what the thought process is behind the "bottom of file scripting" HTML Extension. Why does it work the way it does? JS script tags belong just above the closing body tag, not after the HTML closing tag as it currently works. Please fix it to follow proper web standards. If there is some other reason for having bottom of file scripting that I am unaware of then please add another option to add links to JS just above the closing body tag - call it "add external JS scripting".
  • mlapl1
    mlapl1 Community Member Posts: 350 ♪ Opening Act ♪
    Thank you Darrel. I will take a close look at your example. I successfully inserted a file as "bottom of file scripting". It appears after the closing html file in the produced file - but I cannot access any of its contents- at least I could not trigger a function.

    I agree with your comments to the developers.

     
  • mlapl1
    mlapl1 Community Member Posts: 350 ♪ Opening Act ♪
    Hi again Darrel

    Wonder what I am doing wrong - maybe you could send the .awt file

    The problem I am encountering is that in one or more files, the bottom of script placement is inserting the actual code from the .js file rather than the script tag. It is therefore displaying the content as though it were text and, of course, nothing works. Wonder what I am doing wrong - using Lectora 16 (supposedly patched).

    Thanks!
  • mlapl1
    mlapl1 Community Member Posts: 350 ♪ Opening Act ♪
    I think I have partly worked it out. The file reference must be inserted manually as something like <script src="xyz.js"></script> - it must not be a Lectora inserted file - the inserted .js file should not include <script> tags.

    I am telling this story so others who may have the same problem might benefit a bit. This works with example files - still not functional in my real files.

     
  • klaatu
    klaatu Community Member Posts: 986 ☆ Roadie ☆
    Darn, I usually have the .awt in my zip. Sorry, it's in this one. The bottom of file scripting must have the script tag and source attribute - <script src="main.js"></script> works.

    The actual file you are referencing is just JavaScript, no markup.

    I think you have it right above. Any problems let me know. If you want, give me a specific example of what you want to do and I'll code up a sample.

    Also keep in mind that you have to manually place the JavaScript file in the published content folder. I should have made that clear as well. Once done, though, you wont have to re-publish to change content in the external JS, just reload.

    Darrel
  • mlapl1
    mlapl1 Community Member Posts: 350 ♪ Opening Act ♪
    Thanks Darrel I will look at the example .awt file , but I think I have sorted it out. And yes, you can change the contents of the included file without the need to recompile the whole lesson.

    Basically, I am trying to write a Lectora template for use in language learning - and I want inexperienced teachers to be able to produce their own lessons without touching Lectora. This code will help. Thank you so much!
  • mlapl1
    mlapl1 Community Member Posts: 350 ♪ Opening Act ♪
    Yep... just confirming that this is working correctly. Very useful information. Thanks Darrel.

    Andrew