Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 31st, 2005, 01:25 PM
Registered User
 
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default capturing asp data in clientside javascript

I need to either open a database with javascript or pass an asp array to a javascript array, the following is what I've tried.
In the <head>:

var arrCollageImg = new Array;
arrCollageImg = <%=arrCollageData%>;

Then later in the <body> of the page I will do the following:

<script type="text/javascript">
  for(var i=0;i<arrCollageImg.length - 1; i++){
   document.write("<div>");
   document.write("<img src='images/collage/" + arrCollageImg[4][i] + "'")
   document.write("/<div>");
  }
</script>

Any help will be apprieciated.



Terry Migliorino
 
Old October 31st, 2005, 01:42 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Terry,

How you populate your client-side array will depend on it's dimensions in the server side array.
So for an array in ASP / VBScript you could do something like...
Code:
var arrCollageImg = new Array();
<%
For i = 0 To UBound(arrCollageData)
    Response.Write "arrCollageImg[" & i & "] = """ & Replace(arrCollageData, """", "\""") & """;"
Next
%>
HTH,

Chris

 
Old October 31st, 2005, 01:52 PM
Registered User
 
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Cris.

I'm still getting an error, but it may be in my asp code (which is vbscript).



Terry Migliorino
tmigliorino@usa.net
 
Old November 1st, 2005, 04:26 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Terry,

Can you post your code and the error, please (including where the array is populated in ASP)?

Cheers,

Chris

 
Old November 1st, 2005, 07:19 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

you can connect to a DB with JavaScript, but it is not recommended for security reasons.

also, if arrCollageImg has no elements it cant pick out a UBound.

Picco

www.crmpicco.co.uk
 
Old November 1st, 2005, 03:57 PM
Registered User
 
Join Date: Oct 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is the vbscript that populates arrPageData.

<%
    dim oRS, arrPageData, intArrLen1, intArrLen2

    select case session("sesPageName")
        case "restorationPage"
            strSql = "qryAllRestorationData"
        case "retouchingPage"
            strSql = "qryAllRetouchingData"
        case "collagePage"
            strSql = "qryAllCollageData"
        case "galleryDigitalMenuPage"
            strSql = "qryAllDigitalGalleryData"
        case "galleryPhotoMenuPage"
            strSql = "qryAllPhotoGalleryData"
    end select

    Set oRS = Server.CreateObject("ADODB.Recordset")
    oRS.Open strSql, "dsnCJMGData", adOpenStatic, adLockOptimistic
    arrPageData = oRS.GetRows
    intArrLen = ubound(arrPageData,2)
    oRs.Close
    Set oRs = Nothing
%>

This is the page code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%
    session("sesPageName") = "collagePage"
%>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta name=vs_defaultClientScript content="JavaScript">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="GENERATOR" content="Microsoft FrontPage 6.0" />
<meta name="ProgId" content="FrontPage.Editor.Document" />
<link rel="stylesheet" type="text/css" href="CJMGraphicsPublic.css" />


<title>CJM Graphics Collage Page</title>
<script language="javascript">
<!--
var arrCollageImg = new Array();
<%
--------------------------------------------------
----The error says syntex error at this point ----
--------------------------------------------------
For i = 0 To UBound(arrCollageData,0)
    For j = 0 to UBound(arrCollageData,1)
        Response.Write "arrCollageImg[" & i & "][" & j & "] = """ & Replace(arrCollageData, """", "\""") & """;"
    Next
Next
%>
//-->
</script>
</head>

<body>
<script type="text/javascript">
  for(var i=0;i<arrCollageImg.length - 1; i++){
   document.write("<div>");
   document.write("<img src='images/collage/" + arrCollageImg[4][i] + "'")
   document.write("/<div>");
  }
</script>

</body>

</html>

</html>


Terry Migliorino
tmigliorino@usa.net
 
Old November 2nd, 2005, 05:29 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

i'm having a similar problem:

Code:
    var cabin = "<%=cabin_name%>";
    alert("cabin = " + cabin);
Why wont this work? I have set the value cabin name in the ASP??

Picco

www.crmpicco.co.uk
 
Old November 2nd, 2005, 05:37 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Terry,

The second argument of UBound should be 1 for the first dimension, 2 for the second etc. not 0, 1, ...

HTH,

Chris

 
Old November 2nd, 2005, 07:52 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Picco,

You code looks fine, there must be an error somewhere else in the page - the variable may have gone out of scope / be empty / etc.

Cheers,

Chris

 
Old November 2nd, 2005, 09:10 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

the general structure is

ASP Page:

<%
asp code....

%>
<scrript src="inc.js>

<% cabin_name = "UUUU" %>

can i not get "cabin_name" inside the inc.js file?

www.crmpicco.co.uk





Similar Threads
Thread Thread Starter Forum Replies Last Post
Capturing MSHFlexGrid Data davekrunal46 VB How-To 2 November 30th, 2005 03:00 AM
capturing asp data in clientside javascript Terry Joseph Migliorino Javascript 1 October 31st, 2005 01:42 PM
Global Variables and ClientSide Javascript haricot Classic ASP Basics 3 September 7th, 2005 10:00 AM
Capturing Javascript form via Excel web query helentoomik Javascript 3 November 25th, 2004 12:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.