McHack
asked on
Preloading dynamic images.
I know that this question primarily involves javascript but I'm putting it here because it also involves Coldfusion. The script below will size an image to a specific dimension when it is called from the image tag (<img src...) . I works fine even with dynamic images but the problem is that you see the large version of each image on screen before it gets resized. What I want to know is how can the dynamic images be preloaded so that they will not show until they get resized?
<!--- Add this script to the Head of the document --->
<script language="javascript">
<!--
//Make graphic fit in to specified space
function makeFit(theImg, maxW, maxH){
var oldH, oldW, chngH, chngH
oldH = theImg.height;
oldW = theImg.width;
chngW = oldW/maxW;
chngH = oldH/maxH;
if(chngW > chngH) {
theImg.width = maxW;
}
else if(chngH > chngW){
theImg.height = maxH;
}
else {
theImg.width = maxW;
}
}
//-->
</script>
<!--- Add this code to the Image SRC tag --->
<img src="#Image#" onLoad="makeFit(this, 136, 136)" align="center" border="0" alt="© 2005">
onLoad="makeFit(this, 136, 136)"
<!--- Add this script to the Head of the document --->
<script language="javascript">
<!--
//Make graphic fit in to specified space
function makeFit(theImg, maxW, maxH){
var oldH, oldW, chngH, chngH
oldH = theImg.height;
oldW = theImg.width;
chngW = oldW/maxW;
chngH = oldH/maxH;
if(chngW > chngH) {
theImg.width = maxW;
}
else if(chngH > chngW){
theImg.height = maxH;
}
else {
theImg.width = maxW;
}
}
//-->
</script>
<!--- Add this code to the Image SRC tag --->
<img src="#Image#" onLoad="makeFit(this, 136, 136)" align="center" border="0" alt="© 2005">
onLoad="makeFit(this, 136, 136)"
ASKER
kyle1830
I'm getting an error on the page. I'ts having a problem with the line in the javascript: document.thisImage.style.d isplay = '';
The error says: 'document.thisImage.style' Is null or not an object.
Tried to find out why I'm getting the error but no luck.
McHack
I'm getting an error on the page. I'ts having a problem with the line in the javascript: document.thisImage.style.d
The error says: 'document.thisImage.style'
Tried to find out why I'm getting the error but no luck.
McHack
and you have the div tags around the img?
realized missed some code
document.getElementById('t hisImage') .style.dis play = '';
kyle
document.getElementById('t
kyle
ASKER
Kyle,
With that additional bit of code I'm no longer getting an error but only the first image displays all the other images never load, at least not on screen.
With that additional bit of code I'm no longer getting an error but only the first image displays all the other images never load, at least not on screen.
i was just thinking one image, let me think for a few minutes on this
are your images within a cfoutput from a query?
if so make these changes
<script>
function makeFit(theImg, maxW, maxH, thisName){
var oldH, oldW, chngH, chngH
oldH = theImg.height;
oldW = theImg.width;
chngW = oldW/maxW;
chngH = oldH/maxH;
if(chngW > chngH) {
theImg.width = maxW;
}
else if(chngH > chngW){
theImg.height = maxH;
}
else {
theImg.width = maxW;
}
document.getElementById(th isName).st yle.displa y = '';
}
</script>
<div id="#queryname.currentrow# " style="display:none">
<img src="../images/add.gif" onLoad="makeFit(this, 136, 136, #queryname.currentrow#);" align="center" border="0" alt="© 2005">
</div>
or if you are doing a loop, then just create a counter that names the div and item in the string.
if so make these changes
<script>
function makeFit(theImg, maxW, maxH, thisName){
var oldH, oldW, chngH, chngH
oldH = theImg.height;
oldW = theImg.width;
chngW = oldW/maxW;
chngH = oldH/maxH;
if(chngW > chngH) {
theImg.width = maxW;
}
else if(chngH > chngW){
theImg.height = maxH;
}
else {
theImg.width = maxW;
}
document.getElementById(th
}
</script>
<div id="#queryname.currentrow#
<img src="../images/add.gif" onLoad="makeFit(this, 136, 136, #queryname.currentrow#);" align="center" border="0" alt="© 2005">
</div>
or if you are doing a loop, then just create a counter that names the div and item in the string.
ASKER
Kyle,
Yes there is a loop that builds the table with the images. Here is the code on the page:
<cfset CurrentPage=GetFileFromPat h(GetTempl atePath()) >
<cfparam name="PageNum_photocontest " default="1">
<cfquery name="photocontest" datasource="#appds#">
SELECT * FROM photocontest WHERE Approve = 'YES' ORDER BY ID DESC
</cfquery>
<cfset MaxRows_photocontest=12>
<cfset StartRow_photocontest=Min( (PageNum_p hotocontes t-1)*MaxRo ws_photoco ntest+1,Ma x(photocon test.Recor dCount,1)) >
<cfset EndRow_photocontest=Min(St artRow_pho tocontest+ MaxRows_ph otocontest -1,photoco ntest.Reco rdCount)>
<cfset TotalPages_photocontest=Ce iling(phot ocontest.R ecordCount /MaxRows_p hotocontes t)>
<!-- Auto Resize Window To Image Size -->
<script language="Javascript">
function PopupPic(sPicURL) {
window.open( "popup.htm?"+sPicURL, "",
"resizable=1,HEIGHT=200,WI DTH=200");
}
</script>
<link href="<cfoutput>#siteURL## appDir##si teCSS1#</c foutput>" rel="stylesheet" type="text/css">
<script language="javascript">
<!--
//Make graphic fit in to specified space
function makeFit(theImg, maxW, maxH){
var oldH, oldW, chngH, chngH
oldH = theImg.height;
oldW = theImg.width;
chngW = oldW/maxW;
chngH = oldH/maxH;
if(chngW > chngH) {
theImg.width = maxW;
}
else if(chngH > chngW){
theImg.height = maxH;
}
else {
theImg.width = maxW;
}
document.getElementById('t hisImage') .style.dis play = '';
}
//-->
</script>
<link href="css/forms.css" rel="stylesheet" type="text/css">
<table width="427" align="center" bgcolor="#523F76">
<tr>
<td align="center" class="headlineSM"><a href="rules.cfm" class="headlineSM">Complet e
Rules Here</a></td>
</tr>
</table>
<table width="431" border="0" align="center" cellpadding="0" cellspcing="0">
<!--- Set default value to use in our loop --->
<cfset EndRow=0>
<!--- Set the max number of record to display per row. --->
<cfset ToDisplay=3>
<!--- Start loop and define the stop point --->
<cfloop condition="EndRow lt photocontest.RecordCount">
<!--- Set the conditions for our inner loop --->
<cfset StartRow=EndRow+1>
<cfset EndRow=EndRow+ToDisplay>
<cfif EndRow gt photocontest.RecordCount>
<cfset EndRow = photocontest.RecordCount>
</cfif>
<tr>
<cfloop query="photocontest" startrow="#StartRow#" endrow="#EndRow#">
<td valign="top"><cfoutput><fo rm method="Post" name="photocontest" action="vote.cfm">
<table width="136" border="0" align="center" cellpadding="2" cellspacing="0" bgcolor="##CCCCCC">
<tr height="110" valign="top">
<td valign="top" align="center" bgcolor="##E1DCEC" width="136"><a name="#photocontest.ID#" class="navtext"></a><a href="javascript:PopupPic( '#siteURL# #appDir##P hotoSource ##Image#') "><img src="#siteURL##appDir##Pho toSource## Image#" onLoad="makeFit(this, 136, 136)" align="center" border="0" alt="© 2005 #FirstName# #LastName#"></div></a></td >
</tr>
</table>
<table width="140" border="0" bgcolor="##CCCCCC" align="center" cellpadding="2">
<tr>
<td align="center" valign="top" bgcolor="##523F76" class="bodytext2"><a href="form.cfm?Notes=#Note s#&Image=# Image#" class="bodytext2"><font color="##FFFFFF">Vote </font></a><fo nt color="##FFFFFF">for your favorite</font> </td>
</tr>
<tr>
<td align="center" bgcolor="##E1DCEC" class="bodytext2" valign="top">Test #FirstName# #LastName#<br>#City#, #State#</td>
</tr>
<tr>
<td align="center" bgcolor="##E1DCEC" class="bodytext2" valign="top"><b>Vote: </b> <a href="form.cfm?Notes=#Note s#&Image=# Image#" class="narrowtext">#Notes# </a></td>
</tr>
</table>
</form></cfoutput></td>
</cfloop>
</tr>
</cfloop>
</table>
Yes there is a loop that builds the table with the images. Here is the code on the page:
<cfset CurrentPage=GetFileFromPat
<cfparam name="PageNum_photocontest
<cfquery name="photocontest" datasource="#appds#">
SELECT * FROM photocontest WHERE Approve = 'YES' ORDER BY ID DESC
</cfquery>
<cfset MaxRows_photocontest=12>
<cfset StartRow_photocontest=Min(
<cfset EndRow_photocontest=Min(St
<cfset TotalPages_photocontest=Ce
<!-- Auto Resize Window To Image Size -->
<script language="Javascript">
function PopupPic(sPicURL) {
window.open( "popup.htm?"+sPicURL, "",
"resizable=1,HEIGHT=200,WI
}
</script>
<link href="<cfoutput>#siteURL##
<script language="javascript">
<!--
//Make graphic fit in to specified space
function makeFit(theImg, maxW, maxH){
var oldH, oldW, chngH, chngH
oldH = theImg.height;
oldW = theImg.width;
chngW = oldW/maxW;
chngH = oldH/maxH;
if(chngW > chngH) {
theImg.width = maxW;
}
else if(chngH > chngW){
theImg.height = maxH;
}
else {
theImg.width = maxW;
}
document.getElementById('t
}
//-->
</script>
<link href="css/forms.css" rel="stylesheet" type="text/css">
<table width="427" align="center" bgcolor="#523F76">
<tr>
<td align="center" class="headlineSM"><a href="rules.cfm" class="headlineSM">Complet
Rules Here</a></td>
</tr>
</table>
<table width="431" border="0" align="center" cellpadding="0" cellspcing="0">
<!--- Set default value to use in our loop --->
<cfset EndRow=0>
<!--- Set the max number of record to display per row. --->
<cfset ToDisplay=3>
<!--- Start loop and define the stop point --->
<cfloop condition="EndRow lt photocontest.RecordCount">
<!--- Set the conditions for our inner loop --->
<cfset StartRow=EndRow+1>
<cfset EndRow=EndRow+ToDisplay>
<cfif EndRow gt photocontest.RecordCount>
<cfset EndRow = photocontest.RecordCount>
</cfif>
<tr>
<cfloop query="photocontest" startrow="#StartRow#" endrow="#EndRow#">
<td valign="top"><cfoutput><fo
<table width="136" border="0" align="center" cellpadding="2" cellspacing="0" bgcolor="##CCCCCC">
<tr height="110" valign="top">
<td valign="top" align="center" bgcolor="##E1DCEC" width="136"><a name="#photocontest.ID#" class="navtext"></a><a href="javascript:PopupPic(
</tr>
</table>
<table width="140" border="0" bgcolor="##CCCCCC" align="center" cellpadding="2">
<tr>
<td align="center" valign="top" bgcolor="##523F76" class="bodytext2"><a href="form.cfm?Notes=#Note
</tr>
<tr>
<td align="center" bgcolor="##E1DCEC" class="bodytext2" valign="top">Test #FirstName# #LastName#<br>#City#, #State#</td>
</tr>
<tr>
<td align="center" bgcolor="##E1DCEC" class="bodytext2" valign="top"><b>Vote: </b> <a href="form.cfm?Notes=#Note
</tr>
</table>
</form></cfoutput></td>
</cfloop>
</tr>
</cfloop>
</table>
ok....made change and reposted code as it should be
<cfset CurrentPage=GetFileFromPat h(GetTempl atePath()) >
<cfparam name="PageNum_photocontest " default="1">
<cfquery name="photocontest" datasource="#appds#">
SELECT * FROM photocontest WHERE Approve = 'YES' ORDER BY ID DESC
</cfquery>
<cfset MaxRows_photocontest=12>
<cfset StartRow_photocontest=Min( (PageNum_p hotocontes t-1)*MaxRo ws_photoco ntest+1,Ma x(photocon test.Recor dCount,1)) >
<cfset EndRow_photocontest=Min(St artRow_pho tocontest+ MaxRows_ph otocontest -1,photoco ntest.Reco rdCount)>
<cfset TotalPages_photocontest=Ce iling(phot ocontest.R ecordCount /MaxRows_p hotocontes t)>
<!-- Auto Resize Window To Image Size -->
<script language="Javascript">
function PopupPic(sPicURL) {
window.open( "popup.htm?"+sPicURL, "",
"resizable=1,HEIGHT=200,WI DTH=200");
}
</script>
<link href="<cfoutput>#siteURL## appDir##si teCSS1#</c foutput>" rel="stylesheet" type="text/css">
<script language="javascript">
<!--
//Make graphic fit in to specified space
function makeFit(theImg, maxW, maxH, thisName){
var oldH, oldW, chngH, chngH
oldH = theImg.height;
oldW = theImg.width;
chngW = oldW/maxW;
chngH = oldH/maxH;
if(chngW > chngH) {
theImg.width = maxW;
}
else if(chngH > chngW){
theImg.height = maxH;
}
else {
theImg.width = maxW;
}
document.getElementById(th isName).st yle.displa y = '';
}
//-->
</script>
<link href="css/forms.css" rel="stylesheet" type="text/css">
<table width="427" align="center" bgcolor="#523F76">
<tr>
<td align="center" class="headlineSM"><a href="rules.cfm" class="headlineSM">Complet e
Rules Here</a></td>
</tr>
</table>
<table width="431" border="0" align="center" cellpadding="0" cellspcing="0">
<!--- Set default value to use in our loop --->
<cfset EndRow=0>
<!--- Set the max number of record to display per row. --->
<cfset ToDisplay=3>
<!--- Start loop and define the stop point --->
<cfloop condition="EndRow lt photocontest.RecordCount">
<!--- Set the conditions for our inner loop --->
<cfset StartRow=EndRow+1>
<cfset EndRow=EndRow+ToDisplay>
<cfif EndRow gt photocontest.RecordCount>
<cfset EndRow = photocontest.RecordCount>
</cfif>
<tr>
<cfloop query="photocontest" startrow="#StartRow#" endrow="#EndRow#">
<td valign="top"><cfoutput><fo rm method="Post" name="photocontest" action="vote.cfm">
<table width="136" border="0" align="center" cellpadding="2" cellspacing="0" bgcolor="##CCCCCC">
<tr height="110" valign="top">
<td valign="top" align="center" bgcolor="##E1DCEC" width="136"><a name="#photocontest.ID#" class="navtext"></a><div id="#photocontest.currentr ow#" style="display:none"> <a href="javascript:PopupPic( '#siteURL# #appDir##P hotoSource ##Image#') "><img src="#siteURL##appDir##Pho toSource## Image#" onLoad="makeFit(this, 136, 136,#photocontest.currentr ow#)" align="center" border="0" alt="© 2005 #FirstName# #LastName#"></a></div></td >
</tr>
</table>
<table width="140" border="0" bgcolor="##CCCCCC" align="center" cellpadding="2">
<tr>
<td align="center" valign="top" bgcolor="##523F76" class="bodytext2"><a href="form.cfm?Notes=#Note s#&Image=# Image#" class="bodytext2"><font color="##FFFFFF">Vote </font></a><fo nt color="##FFFFFF">for your favorite</font> </td>
</tr>
<tr>
<td align="center" bgcolor="##E1DCEC" class="bodytext2" valign="top">Test #FirstName# #LastName#<br>#City#, #State#</td>
</tr>
<tr>
<td align="center" bgcolor="##E1DCEC" class="bodytext2" valign="top"><b>Vote: </b> <a href="form.cfm?Notes=#Note s#&Image=# Image#" class="narrowtext">#Notes# </a></td>
</tr>
</table>
</form></cfoutput></td>
</cfloop>
</tr>
</cfloop>
</table>
<cfset CurrentPage=GetFileFromPat
<cfparam name="PageNum_photocontest
<cfquery name="photocontest" datasource="#appds#">
SELECT * FROM photocontest WHERE Approve = 'YES' ORDER BY ID DESC
</cfquery>
<cfset MaxRows_photocontest=12>
<cfset StartRow_photocontest=Min(
<cfset EndRow_photocontest=Min(St
<cfset TotalPages_photocontest=Ce
<!-- Auto Resize Window To Image Size -->
<script language="Javascript">
function PopupPic(sPicURL) {
window.open( "popup.htm?"+sPicURL, "",
"resizable=1,HEIGHT=200,WI
}
</script>
<link href="<cfoutput>#siteURL##
<script language="javascript">
<!--
//Make graphic fit in to specified space
function makeFit(theImg, maxW, maxH, thisName){
var oldH, oldW, chngH, chngH
oldH = theImg.height;
oldW = theImg.width;
chngW = oldW/maxW;
chngH = oldH/maxH;
if(chngW > chngH) {
theImg.width = maxW;
}
else if(chngH > chngW){
theImg.height = maxH;
}
else {
theImg.width = maxW;
}
document.getElementById(th
}
//-->
</script>
<link href="css/forms.css" rel="stylesheet" type="text/css">
<table width="427" align="center" bgcolor="#523F76">
<tr>
<td align="center" class="headlineSM"><a href="rules.cfm" class="headlineSM">Complet
Rules Here</a></td>
</tr>
</table>
<table width="431" border="0" align="center" cellpadding="0" cellspcing="0">
<!--- Set default value to use in our loop --->
<cfset EndRow=0>
<!--- Set the max number of record to display per row. --->
<cfset ToDisplay=3>
<!--- Start loop and define the stop point --->
<cfloop condition="EndRow lt photocontest.RecordCount">
<!--- Set the conditions for our inner loop --->
<cfset StartRow=EndRow+1>
<cfset EndRow=EndRow+ToDisplay>
<cfif EndRow gt photocontest.RecordCount>
<cfset EndRow = photocontest.RecordCount>
</cfif>
<tr>
<cfloop query="photocontest" startrow="#StartRow#" endrow="#EndRow#">
<td valign="top"><cfoutput><fo
<table width="136" border="0" align="center" cellpadding="2" cellspacing="0" bgcolor="##CCCCCC">
<tr height="110" valign="top">
<td valign="top" align="center" bgcolor="##E1DCEC" width="136"><a name="#photocontest.ID#" class="navtext"></a><div id="#photocontest.currentr
</tr>
</table>
<table width="140" border="0" bgcolor="##CCCCCC" align="center" cellpadding="2">
<tr>
<td align="center" valign="top" bgcolor="##523F76" class="bodytext2"><a href="form.cfm?Notes=#Note
</tr>
<tr>
<td align="center" bgcolor="##E1DCEC" class="bodytext2" valign="top">Test #FirstName# #LastName#<br>#City#, #State#</td>
</tr>
<tr>
<td align="center" bgcolor="##E1DCEC" class="bodytext2" valign="top"><b>Vote: </b> <a href="form.cfm?Notes=#Note
</tr>
</table>
</form></cfoutput></td>
</cfloop>
</tr>
</cfloop>
</table>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Kyle,
I tried them both and the first one worked and the second didn't. I only needed one to work so I'm all set. Thank you for the speedy solution.
I tried them both and the first one worked and the second didn't. I only needed one to work so I'm all set. Thank you for the speedy solution.
glad to help ..good luck
<script language="javascript">
<!--
//Make graphic fit in to specified space
function makeFit(theImg, maxW, maxH){
var oldH, oldW, chngH, chngH
oldH = theImg.height;
oldW = theImg.width;
chngW = oldW/maxW;
chngH = oldH/maxH;
if(chngW > chngH) {
theImg.width = maxW;
}
else if(chngH > chngW){
theImg.height = maxH;
}
else {
theImg.width = maxW;
}
document.thisImage.style.d
}
//-->
</script>
<!--- put the div around the image and set the display to hide it --->
<div id="thisImage" style="display:none">
<img src="#Image#" onLoad="makeFit(this, 136, 136)" align="center" border="0" alt="© 2005">
</div>