skeatt
12-09-2002, 05:58 AM
This code was originaly posted by Ardios: however I've taken a lot of bits out to make it easier to understand. (like styles)
Original thread (http://www.codingforums.com/showthread.php?s=&threadid=5504&highlight=dynamic%2Fvariable)
I don't understand how the images are loaded and how the function in the for loop is activated... pic[i].myObject.onload = new Function(
I guess I'm having a hard time understanding the need for "myObject: null" it's function and how the ":" bit helps the array.
(arrays were always my weak spot):(
Is there another way off writing the opener?
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">
var HTML;
var newWindow = null;
var pic = new Array();
var path = 'images/'; //images directory
/* URLS below, as shown */
pic[0] = {
src: 'cover.jpg',
myObject: null,
width: null,
height: null,
title: 'Sammy',
loaded: false
}
pic[1] = {
src: 'A.E.Newman.jpg',
myObject: null,
width: null,
height: null,
title: 'Gulbuddin Hekmatyar',
loaded: false
}
pic[2] = {
src: '777d.jpg',
myObject: null,
width: null,
height: null,
title: 'Dicky',
loaded: false
}
// preloader
for (var i=0; i<pic.length; ++i) {
pic[i].myObject = new Image();
pic[i].myObject.onload = new Function(
'pic['+i+'].width=this.width;' +
'pic['+i+'].height=this.height;' +
'pic['+i+'].loaded=true;'
);
pic[i].myObject.src = path + pic[i].src;
}
function graphwin(picObj) {
if (newWindow && !newWindow.closed) newWindow.close(); // close the window each time.
if (!arguments.length) return; // if there is no parameter passed, do nothing
if (picObj.loaded) {
HTML = '';
HTML += '<html><head><title>'+picObj.title+'</title><script type="text/javascript">';
HTML += 'onload=function(){self.focus()};';
HTML += '<\/script></head>';
HTML += '<body marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" bgcolor="steelblue">';
HTML += '<img border="0" width="'+picObj.width+'" height="'+picObj.height+'" ';
// HTML += 'src="'+ path + picObj.src+'">';
HTML += 'src=" ' + picObj.myObject.src+' ">';
HTML += '<table width="100%" cellspacing="0" cellpadding="0" border="1"><tr>';
HTML += '<td align="center" bgcolor="steelblue">';
HTML += '<a href="javascript:self.close()">close</a>';
HTML += '</td></tr></table></body></html>';
var Gleft = screen.availWidth/2 - picObj.width/2;
var Gtop = screen.availHeight/2 - picObj.height/2;
var winFeatures = 'width='+picObj.width+',height='+(picObj.height+16); // Add Title bar to allow room for close link.
winFeatures +=',left='+Gleft+',top='+Gtop+',status=0';
newWindow = window.open('javascript: opener .HTML','newWindow',winFeatures); // HTML was defined as a variable
// alert(picObj.myObject.src);
}
}
</script>
</head>
<body>
<a href="javascript:void graphwin(pic[0])">Sam </a><br><br>
<a href="javascript:void graphwin(pic[1])">Gul </a><br><br>
<a href="javascript:void graphwin(pic[2])">Dick </a><br><br><br>
<a href="javascript:void graphwin()">close window</a>
</body>
</html>
Original thread (http://www.codingforums.com/showthread.php?s=&threadid=5504&highlight=dynamic%2Fvariable)
I don't understand how the images are loaded and how the function in the for loop is activated... pic[i].myObject.onload = new Function(
I guess I'm having a hard time understanding the need for "myObject: null" it's function and how the ":" bit helps the array.
(arrays were always my weak spot):(
Is there another way off writing the opener?
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">
var HTML;
var newWindow = null;
var pic = new Array();
var path = 'images/'; //images directory
/* URLS below, as shown */
pic[0] = {
src: 'cover.jpg',
myObject: null,
width: null,
height: null,
title: 'Sammy',
loaded: false
}
pic[1] = {
src: 'A.E.Newman.jpg',
myObject: null,
width: null,
height: null,
title: 'Gulbuddin Hekmatyar',
loaded: false
}
pic[2] = {
src: '777d.jpg',
myObject: null,
width: null,
height: null,
title: 'Dicky',
loaded: false
}
// preloader
for (var i=0; i<pic.length; ++i) {
pic[i].myObject = new Image();
pic[i].myObject.onload = new Function(
'pic['+i+'].width=this.width;' +
'pic['+i+'].height=this.height;' +
'pic['+i+'].loaded=true;'
);
pic[i].myObject.src = path + pic[i].src;
}
function graphwin(picObj) {
if (newWindow && !newWindow.closed) newWindow.close(); // close the window each time.
if (!arguments.length) return; // if there is no parameter passed, do nothing
if (picObj.loaded) {
HTML = '';
HTML += '<html><head><title>'+picObj.title+'</title><script type="text/javascript">';
HTML += 'onload=function(){self.focus()};';
HTML += '<\/script></head>';
HTML += '<body marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" bgcolor="steelblue">';
HTML += '<img border="0" width="'+picObj.width+'" height="'+picObj.height+'" ';
// HTML += 'src="'+ path + picObj.src+'">';
HTML += 'src=" ' + picObj.myObject.src+' ">';
HTML += '<table width="100%" cellspacing="0" cellpadding="0" border="1"><tr>';
HTML += '<td align="center" bgcolor="steelblue">';
HTML += '<a href="javascript:self.close()">close</a>';
HTML += '</td></tr></table></body></html>';
var Gleft = screen.availWidth/2 - picObj.width/2;
var Gtop = screen.availHeight/2 - picObj.height/2;
var winFeatures = 'width='+picObj.width+',height='+(picObj.height+16); // Add Title bar to allow room for close link.
winFeatures +=',left='+Gleft+',top='+Gtop+',status=0';
newWindow = window.open('javascript: opener .HTML','newWindow',winFeatures); // HTML was defined as a variable
// alert(picObj.myObject.src);
}
}
</script>
</head>
<body>
<a href="javascript:void graphwin(pic[0])">Sam </a><br><br>
<a href="javascript:void graphwin(pic[1])">Gul </a><br><br>
<a href="javascript:void graphwin(pic[2])">Dick </a><br><br><br>
<a href="javascript:void graphwin()">close window</a>
</body>
</html>