[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1113
  • Last Modified:

rotate images asp, vbscript & javascript

I have pieced together some code that I knew wouldn't work, but thought it would give someone an idea of what I'm trying to do.
I'm reading in a database, building an array, then trying to use that array in some javascript to rotate the images (from the array from the database).
The html is being displayed along with the first 3 ad images (from the databse), then no alerts are showing so no javascript is being executed, but more importantly, I don't think this code will work from the client/server side timing.
Even though I have onload(rotate())
I probably need two separate asp's, but any help would be appreciated.

<html>
      <!--  #include virtual = _private/connect.asp -->


<FORM method="POST"  ID="Home" >
<body onload="rotate()">
<table width=940><tr><td width=940 valign=top align=center>
<%  
  co="comp"
  ctr = 0
  SQLBanners = "SELECT * FROM Ads where key='Home'" 
  SQL_Rs.Open SQLBanners, SQL_Cn,3
  IF NOT(SQL_Rs.EOF) THEN
    Banner = SQL_Rs.getrows 
    ctr = ubound(Banner,2)
  end if
  SQL_Rs.Close

  Dim ximg()
  for i = 0 to ctr
     ximg(0,i) = "emenus/"+co+"/bc"+Banner(4,i)
     ximg(1,i) = Banner(3,i)
  next
'Tried this commented code but didn't work
'Response.Write "<script type=""text/javascript"">" + vbCrLf
'Response.Write "var adImages = new Array("

'For j = 0 To UBound(ximg,2)
'    If j > 0 Then
'        Response.Write ","
'    End If
'    Response.Write """" & ximg(0,j) & """"
'Next 

'Response.Write ");" & vbCrLf
'Response.Write "</script>"


'Response.Write "<script type=""text/javascript"">" & vbCrLf
'Response.Write "var adURL = new Array(" 

'For j = 0 To UBound(ximg,2)
'    If j > 0 Then 
'        Response.Write "," 
'    End If 
'    Response.Write """" & ximg(1,j) & """" 	
'Next 
'Response.Write ");" & vbCrLf 
'Response.Write "</script>"

%>
<script type=""text/javascript"">
// alert not being displayed
alert("0-here");
var adImages = new Array();
var adURL = new Array();

<%
For j = 0 To UBound(ximg,2)
    If j > 0 Then
        Response.Write ","
    End If
    response.write "adImages[" & i & "] = " & ximg(0,j) & ";" & vbCrLf
Next 
For j = 0 To UBound(ximg,2)
    If j > 0 Then
        Response.Write ","
    End If
    response.write "adURL[" & i & "] = " & ximg(1,j) & ";" & vbCrLf
Next 
%>

var thisAd = 0;
var imgCt = adImages.length;
alert("1-imgct - "imgCt);
function rotate() {
// never being executed
alert("2-imgCt - "imgCt);
	  thisAd++;
	  if (thisAd == imgCt) thisAd = 0;
	  document.cycle.src = "emenus/"+'<%=sCo%>'+"/"+adImages[thisAd];
      setTimeout("rotate()", 4000);   // 4 seconds
}
function rotateLink() { window.open(adURL[thisAd]) }
</script>
<%
' display ads on every page header, rotating
if isArray(Banner) then  

   for i = 0 to 2
' only display 3 
     if j < 3 then
        imgnm = ximg(0,i)
        slink = ximg(1,i)
  %> 
      <a onClick="rotateLink()" target=_blank>
      <img  border=0 name="cycle" id="cycle" height="120"></a>&nbsp;&nbsp;&nbsp;&nbsp;
  <%
     end if
     j=j+1
   next
 
 end if 
end if %>
</td></tr></table>
</form>
</body>
</html>

Open in new window

0
dcass
Asked:
dcass
  • 6
  • 3
1 Solution
 
worthyking1Commented:
Javascript is not like ASP, you cannot just slap it on the page. It only executes from a trigger such as an onLoad, onClick, onFocus etc. etc.  So, even if your rotate() function did execute onLoad it does not have the Javascript array built.

You should put ALL of your Javascript into a single function and then trigger that with your <body onLoad="Javascript:rotate()">.  Or put the array code and other neccessary Javascript into separate functions, and call them as needed from within rotate().

I don't see any issues with client/server timing because your ASP code will execute first, pull your DB data etc. so by the time the Javascript executes on the client-side all your values will be available.
0
 
dcassAuthor Commented:
That's the problem - can't load the array from the database in js and there is no other trigger except onload.
 
0
 
dcassAuthor Commented:
But doesn't Onload execute after all the asp code?  That would work - pass in the array.
Got any code on that?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
worthyking1Commented:
Try using a placeholder image that loads before your first ad, and put an onLoad trigger on that image.

But still you have javascript code that is outside of any functions (lines 52 thru 74), so none of that code is ever going to execute and thus none of your functions will work either (because the javascript arrays will be missing).

Javascript is tricky. I would suggest using Firebug in FF or the Javascript console in Google Chrome to troubleshoot your code at runtime.
0
 
dcassAuthor Commented:
I did use a placeholder image with a trigger - an ID to replace.  I understand about getting it all into a function.
Please tell if this will work before I spend a lot of time on it.
http://www.aspfree.com/c/a/ASP/Converting-VBScript-Array-to-Javascript-Array-in-the-same-ASP-Page-1D-and-2D-by-Anand-Venkatraman/
0
 
worthyking1Commented:
That looks like a feasible way to do it.
0
 
dcassAuthor Commented:
I am working on this - please do not close.
0
 
dcassAuthor Commented:
Finally  -  Got a solution that works:

<%
  co=request.cookies("webuser")("company")
  ctr = 0
  SQLBanners = "SELECT * FROM yourfile" 
  SQL_Rs.Open SQLBanners, SQL_Cn,3
  IF NOT(SQL_Rs.EOF) THEN
    Banner = SQL_Rs.getrows 
    ctr = ubound(Banner,2)
  end if
  SQL_Rs.Close
    
if ctr > 0 then 
  Dim arrImg(99)
  Dim arrLink(99)
  for i = 0 to ctr
     arrImg(i) = "emenus/"+co+"/"+Banner(4,i)
     arrLink(i) = Banner(3,i)
  Next


  Call ConvertToJSArray1D(arrImg,"arrImg")
  Call ConvertToJSArray1D(arrLink,"arrLink")
  
  
sImg2 = arrImg(1)
si = "1"
if ctr >= 1 then %>
     <a href="<%=arrLink(0)%>" id="link0" target=_blank>
     <img src="<%=arrImg(0)%>"  border=0 name="cycle0" id="cycle0" height="120"></a>&nbsp;&nbsp; 
<% else 
   sImg2 = arrImg(0) 
   sLink2 = arrLink(0)
   si = "0" 
end if %>
<% if ctr > 0 then %>
     <a href="<%=arrLink(si)%>" id="link1" target=_blank>
     <img src="<%=sImg2%>"  border=0 name="cycle1" id="cycle1" height="120"></a>&nbsp;&nbsp;
<% end if %>
<% if ctr >= 2 then %> 
     <a href="<%=arrLink(2)%>" id="link2" target=_blank>
     <img src="<%=arrImg(2)%>"  border=0 name="cycle2" id="cycle2" height="120"></a>
<% end if 
end if %>

     
<SCRIPT LANGUAGE="JAVASCRIPT">
  var aIl = arrImg.length;
  if (aIl > 2) {
    setTimeout(function(){rotate1(3,aIl)}, 8000);
  }
  function rotate1(j,aIl) {
      var k = j;
      var p = aIl;
      var hitctr1="0";
      for (i=k;i<p;i++) {
        if (arrImg[i] != "") { 
          k=i;  
	      document.getElementById("link0").href=arrLink[k];
          document.getElementById('cycle0').src=arrImg[k];
          hitctr1="1";
          i=p;
        }
      }
      if (hitctr1=="0") {
	    document.getElementById("link0").href=arrLink[0];
        document.getElementById('cycle0').src=arrImg[0];
        k=0;
      }
      k = k+1;
      if (k>=p) { k=0; }
      var hitctr2 = "0";
      for (i=k;i<p;i++) {
        if (arrImg[i] != "") { 
          k=i;
	      document.getElementById("link1").href=arrLink[k];
          document.getElementById('cycle1').src=arrImg[k];
          hitctr2 = "1";
          i=p;
        }
      }
      if (hitctr2=="0") {
        k=0;
	    document.getElementById("link1").href=arrLink[0];
        document.getElementById('cycle1').src=arrImg[0];
      }
      k = k+1;
      if (k>=p) { k=0; }
      var hitctr3 = "0";
      for (i=k;i<p;i++) {
        if (arrImg[i] != "") { 
          k=i; 
	      document.getElementById("link2").href=arrLink[k];
          document.getElementById('cycle2').src=arrImg[k];
          hitctr3="1";
          i=p;
        }
      }
      if (hitctr3=="0") {
        k=0;
	    document.getElementById("link2").href=arrLink[0];
        document.getElementById('cycle2').src=arrImg[0];
      }

     
      k=k+1;
      if (k>=p) { k=0; }
         setTimeout(function(){rotate1(k,p)}, 8000);
      if (svK==k) { k=k+1; }
      
  }
  function rotateLink(i) { 
    alert("here");
    var x = i;
    window.open(arrLink[x]); 
    } 

  
</SCRIPT>



THEN vb2JSarray1D.inc:
<%
'***************************************************
'Name        : VB2JSarray2D.inc
'Description    : Include File used to convert a VBScript Two Dimensional Array to Two Dimensional Javascript Array
'Developer    : Anand Venkatraman
'Creation Date    : 03/31/2000
'***************************************************

'This function converts a VBScript Array to Javascript Array(Two Dimensional array only)
'Inputs are VBScript array and Javascript array name
'For the sake of variable names "mismatch", "vb2js" is prefixed to all the variables
'For eg. in ASP page you might call this function as "Call ConvertToJSArray(arrUserList,"arrUserList")
'When this function is called immediately Javascript array is created and written to the web page
'So we cannot use Response.Redirect after calling this function. You can View the Source of HTML page
'to see the Javascript array.

Function ConvertToJSArray2D(VBArray , ArrayName)

Dim vb2jsRow, vb2jsCol , vb2jsStr, vb2jsi, vb2jsj
vb2jsRow = Ubound(VBArray,1)
vb2jsCol = Ubound(VBArray,2)
%>
<SCRIPT LANGUAGE = 'JAVASCRIPT' >
var vb2jsi,vb2jsj
<%=ArrayName%> = new Array(<%=vb2jsRow+1%>);
for (vb2jsi=0; vb2jsi < <%=vb2jsRow+1%>; vb2jsi++)
{
    <%=ArrayName%>[vb2jsi] = new Array(<%=vb2jsCol+1%>)
for (vb2jsj=0; vb2jsj < <%=vb2jsCol+1%>; vb2jsj++) <%=ArrayName%>[vb2jsi][vb2jsj] = " "
}
</SCRIPT>
<%
Response.Write("<SCR"&"IPT LANGUAGE = 'JAVASCRIPT' >"&chr(13))
for vb2jsi=0 to vb2jsRow
for vb2jsj=0 to vb2jsCol
    vb2jsstr = "VBArray("&vb2jsi&","&vb2jsj&")"
%>    
    <%=ArrayName%>[<%=vb2jsi%>][<%=vb2jsj%>] = "<%=trim(eval(vb2jsstr))%>"
<%
Next
Next
Response.Write("</SCR"&"IPT>")
End Function

%>

Open in new window

0
 
dcassAuthor Commented:
Took a lot of research but this will work for anyone
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now