0 votes
by (250 points)
<<script>>
			if (document.images) {
				$img1 = new Image();
			
				$img1.src = "http://hyperstructuregames.com/wp-content/uploads/2017/09/StartScreen.jpg";
			}
<</script>>

When I run the script above I get an error stating that $img1 is not defined. Is that because $img1 is a temporary variable within the javascript, which is not supported in the <<script>> tag?

1 Answer

0 votes
by (68.6k points)
edited by

It's because there's no auto-global property or variable within a parent scope by that name and you haven't declared a local variable by that name.  It literally does not exist, thus the error.

If a local variable is not what you wanted, then please clarify your intent.

Assuming you did want a local variable, try something like the following:

<<script>>
if (document.images) {
	var img1 = new Image();
	img1.src = "http://hyperstructuregames.com/wp-content/uploads/2017/09/StartScreen.jpg";
}
<</script>>

NOTE: By convention, a leading dollar sign on variable names in JavaScript generally signifies that the variable in question is an instance of some library object—famously jQuery, though other libraries use it as well.  That being the case, I do not recommend using a leading dollar sign on variable names in JavaScript unless you are following that convention.


PS: Please tag your questions with the name+major version of both the compiler you're using (e.g. twine1, twine2, tweego1) and story format (e.g. harlowe2, sugarcube2), because it can, and often does, make a difference to the answers you'll receive and how quickly you receive them.  I'd also suggest specifying the full version of each somewhere, either as additional tags (e.g. twine2-1-3, sugarcube2-1-8) or within the post, because that can matter too.

...