Scope

Date: 2019-06-11 08:20 am (UTC)
tornir: Cat under a car, surrounded by tools. (Cat Mechanic)
From: [personal profile] tornir
That was the root of the problem. That was why I was messing around with cookies in an attempt to stop variables disappearing before I could use them.

This works:
(Simple, small, and doesn't need cookies)
/files/images.js:
// images.js

	var JS_First_Image= "files/bomb.png", JS_Second_Image= "files/eye.png";
	var First_Image_x_1, First_Image_y_1;
	var Second_Image_x_1, Second_Image_y_1;

function init() {
// Write the first image invisibly.
	document.getElementById("image_init_1").innerHTML= '<img id="First_one" alt="Move along" src="'+JS_First_Image+'" style="width:0px; height:0px;" onload="init_2()">';
}

function init_2() {
// When the first image has loaded, write the second image invisibly.
	document.getElementById("image_init_2").innerHTML= '<img id="Second_one" alt="Nothing to see" src="'+JS_Second_Image+'" style="width:0px; height:0px;" onload="get_sizes()">';
}

function get_sizes() {
// When both images have loaded, get their dimensions.
	First_Image_x_1= document.getElementById("First_one").naturalWidth;
	First_Image_y_1= document.getElementById("First_one").naturalHeight;
	Second_Image_x_1= document.getElementById("Second_one").naturalWidth;
	Second_Image_y_1= document.getElementById("Second_one").naturalHeight;
	document.getElementById("test1").innerHTML= "Image1: "+First_Image_x_1+" "+First_Image_y_1+" Image2: "+Second_Image_x_1+" "+Second_Image_y_1;
	// Proceed from here.
}


Output (for both FF & Chrome):

Image1: 49 50 Image2: 34 34
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

javascript: The cover of O'Reilly's Javascript book. (Default)
Javascript Coding!

June 2019

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829
30      

Style Credit

Expand Cut Tags

No cut tags