Skip to main content
Inspiring
July 4, 2008
Question

CF Array and JavaScript

  • July 4, 2008
  • 21 replies
  • 2405 views
Is it possible to manipulate arrays created in coldfusion with JavaScript?
All I'm trying to do is update a count (array length) dynamically everytime something is added to the array. So I got the brilliant idea to add the data submitted using JS and do a dynamic count update at the same time. But what I get is the JS error saying the array is undefined.

Suggestions?
This topic has been closed for replies.

21 replies

Inspiring
July 12, 2008
Dinghus, did you try the example? Is that what you were looking for?
Inspiring
July 11, 2008
Ok, so I'm not totally positive this is what you want, but I've created some code that I think achieves what you are looking for. The only thing I didn't include is a function for getting your js array values into a form field for passing to the server. It's pretty easy, if you aren't familiar I can give you code for that too.

Hopefully this is what you are looking for. See attached code.

I have also put this on my website to show you. Link is here: Demo
Inspiring
July 10, 2008
> Technically ALL arrays are 1D.

Say what?

Is this some obscure reference to the schema of the underlying data storage
mechanism?

--
Adam
Inspiring
July 10, 2008
> Technically JS arrays are 1D arrays [...].

Sure.


> The difference is that in a typical 2D array, all elements are
> bound by type;

Only in a typeful language. It's quite possible to have multidimensional
arrays in typeless languages, each data point of which is an arbitrary
notional data type.

> in JS there is no such boundary. One array may contain numbers,
> text, other arrays, images, et al.

I would more say that the definition of a true multi-dimensional array is
that each element at every index of the first [n-1] dimensions is an array.
The final dimension contains the data points. The rules of the data types
of these points is dictated by the typefulness of the language.

Which is all well and good, but nothing to do with the issue @ hand, I
think we can all agree ;-)

--
Adam
DinghusAuthor
Inspiring
July 10, 2008
Technically ALL arrays are 1D. But that is nitpicking. :)

Anyway, I'd love to see an example of #1.
July 10, 2008
On 07/08/2008 11:19:50 AM Dinghus wrote: "Hmmm first, JS does have 2D arrays, just a bit of playing around to do it. "

Technically JS arrays are 1D arrays that may (or may not) contain other arrays as elements. The difference is that in a typical 2D array, all elements are bound by type; in JS there is no such boundary. One array may contain numbers, text, other arrays, images, et al.

But... back to the original question:
I use two different methods of loading CF data into arrays:

1) I load a hidden [XHTML] element with the CF array contents, and then read/manipulate that element as required. Note that array is inserted as an XML document - which meets DOM compliance requirements - and that JS is used to parse the XML as needed.

2) When #1 is not practical, I use Ajax to execute CF scripts to load data directly into JS arrays and then - after the JS arrays are manipulated - use Ajax again to update the server.

Inspiring
July 8, 2008
Dinghus wrote:
> I use wddx to save the array to the client variable.

It can be used to directly build an JavaScript array if desired.

<html>
<head>
<script type="text/javascript">
<cfwddx input="#myCFArray#" toplevelvariable="myJSArray"
action="cfml2js">
</script>
</head>
...
</html>

Run a page like this and view source in a browser and see a nice JSArray
all ready for minuplation. I leave it up to you to write JavaScript
that then does something the the myJSArray structure in the client.
DinghusAuthor
Inspiring
July 8, 2008
I use wddx to save the array to the client variable.
Inspiring
July 8, 2008
> Maybe I can manipulate cookies in the same way, tho I'm thinking sandbox rules
> probably will prevent that.

Before the notion of AJAX was around, Jim Davis wrote an essay on how to do
async comms with a GIF and a cookie.

http://www.depressedpress.com/Content/Development/JavaScript/Articles/GIFAsPipe/Index.cfm

This covers the cookie manipulation you mention.


> Obviously JS and CF can not communicate in real time but assuming they can not
> communicate is incorrect.

Well I think the point is that they cannot communicate *directly*: they
need to use HTTP. Just like you and I can't just talk to each other
directly: we need a forum.

Really a lot of people on these forums seem to think "CF is a computer
language which has something to do with making my web page do stuff...
JavaScript is a computer language which has something to do with making my
web page do stuff. That's pretty much the same: they must just
intrinsically be able to play with each other's code". And they don't seem
to "get" the whole client / server thing. I think from your earlier posts
in this thread it would be fair enough for people to be saying "well...
no... it doesn't work like that". I don't think anyone was assuming it's
not possible, I think they might have been giving the "Client/Server 101"
answer, before moving on to the "Client/Server 102: why that is the answer"
spiel. Quite possibly they could have skipped to the latter, in this case.

--
Adam
Inspiring
July 8, 2008
Dinghus wrote:
>
> Obviously JS and CF can not communicate in real time but assuming they can not
> communicate is incorrect.
>

But how they communicate, and the limitations imposed on this, are very
important.

If it has not been suggested yet, have you looked into the <cfwddx...>
tag? It has an option to convert a ColdFusion data into JavaScript
data. I've used it in the past to pass array data to the client where
it can be manipulated by code running in the browser. You then need to
someway pass the data back to the server in a following request where it
can be used to update the original data.