I am aiming to cross-reference a variable across a.js and a.html file.
The first of the two scripts is written in jQuery javascript, the second in vanilla javascript.
I intend that if the code.jquery.com CDN go offline, I can fallback on some good ol' vanilla.
I have an html segment as follows:
<script src="subsidiary_jquery.js"></script>
<script>
if (subsidiary_jquery.active == true) {
console.log("adventure_jquery active true");
} else {
console.log("adventure_jquery active false");
//Something to be done, perhaps import statement?
}
//Blank space
var array = [];
console.log("Do some stuff");
//etcetera
</script>
The first script tag pair references a script within the current directory/folder, where my jQuery code rests.
The second script tag pair contains some pseudo-code which (tries and fails at) provides conditional statements for an appropriate course of action, on the event that the CDN go offline and my jQuery code be rendered useless. Obviously, the subsidiary_jquery.active
piece references a non-existant object variable/method or a function sub-function - neither of which I have in place.
I want to - dumbed down - say "Get this script, search inside it, if active is true, do this - if not, do that.", however, seen in my attempts, this doesn't work too well. Well, at all.
Uncaught ReferenceError: script is not defined at index.html:<line number>
I am thoroughly interested in this topic, because... it's my work. So, I took some time to scoll through a list of related questions and found this:
Typescript global variable across files.
My knowledge registers Typescript as an 'improved' JavaScript, as far as I can gather. And I also know that Typescript uses slightly differed syntax than to JavaScript, so some stuff in Typescript isn't compatible with JavaScript. Yada-yada - the /// <reference path="..." />
statement allows for cross-reference between files, so 'shared' variables can be used. But, this isn't JavaScript, gawsh darn it. (Is there something , vanilla, similar to this?)
As of current I do not have many viable solutions. However, to save myself from being completely stuck, I have implemented a commented 'just in case' script tag which refers to a stored-offline jQuery library, just in case.
Having this instead of a complicated variable-detection system would be easier to control and maintain, which to many would agree, but I do not want to go into the source file to adjust certain values just because this-so script has stopped working. This isn't a question about, "Should I?", this is a question about, "What's this, what is good about that, and why?", answer-suggestions are appreciated.
Thank you for reading.