PDA

For continued disscussion on this topic : pass coldfusion variable to javascript?



oberon
26th September 2002, 08:06
is it possible to pass a coldfusion variable to be used in javascript?
I have tried the following
but not only doesn't it work but I get javascript error


<script language="JavaScript">
<!--
var oldvar = #BIN.pcdate#;
-->
</script>

thanks

piglet
26th September 2002, 14:03
I've never used CF - but I expect you just need some quotes unless BIN.pcdate is a number:


<script language="JavaScript">
<!--
var oldvar = "#BIN.pcdate#";
-->
</script>

oberon
27th September 2002, 03:33
BIN.pcdate is a date but putting either double or single quotes around it will all return javascript errors

Any other ideas?

thanks

piglet
27th September 2002, 10:23
Please can you post a copy of the generated javascript - or a link to the test page - it'll be easier to see what's going on in context.

Thanks

oberon
30th September 2002, 01:09
Heres what I am working on
http://67r761s/Quality/Bins/index.cfm
you'll need to log in to see what the error is
"ttrinh/password"

Thanks

Ulaire Smiley
30th September 2002, 01:21
It appears that you have a server installed on your computer, because the address you gave us is not a .com, .net, .org, etc. and we can't access it. Can you put it on the web?

- Smiley :)

oberon
30th September 2002, 01:36
Hi
Sorry I forgot I am behind a firewall and don't have access to our public site

Heres the part of code for the dropdown date :

<td valign="top" class="reqd">Planned Completion:&nbsp;&nbsp;</td>
<td valign="top">
<cfif BIN.pcdate neq "">
<cfset defaultdate = BIN.pcdate>
<cfelse>
<cfset defaultdate = "">
</cfif>
<cfif thisBIN.canEdit eq "false">
<cfset disabledflag = "yes">
<cfelse>
<cfset disabledflag = "no">
</cfif>
<cf_datedropdowns name="pcDate" startyear="1997"
endyear="2010" selected="#defaultdate#"
noneoption=" " dateformat="dd-mmm-yyyy"
show="no" class="reqdtext" disabled="#disabledflag#">
</td>
and heres my code for the checking for the date applied before someone hits the save button its in a dirfferent file:

<cfif thisBIN.canEdit eq "true">
<tr><td><img src="images/shim.gif" width="1" height="#buttonspace#" border="0"></td></tr>
<td>&nbsp;</td>
<cfif BIN.pcdate neq "">
<cfif BIN.pcdate neq #bin.pcdate#>
<td><input type="Button" value="Update;validate()" class="lhsbuttonsave" onclick="popup=window.open('changePCD.cfm?bin=#attributes.bin#','changePCDbox','width=350,height=190,scrollba rs=no,resizable=yes');self.focus();"></td>
<cfelse>
<td><input type="button" value="Save" class="lhsbuttonsave" onclick="validate(#BIN.pcdate#)"></td>
</cfif>
</tr>
</cfif>
<td>bin.pcdate: #bin.pcdate#<br>
BIN.pcdate : BIN.pcdate
<!-- <cfset olddate = #BIN.pcdate#>-->
</td>
<script language="JavaScript">
<!--
var oldvar = "#BIN.pcdate#";
-->
</script>
</cfif>
<script language = "JavaScript">
function validate(oldpcd){
//var oldpcd="";
var newpcd="";
if (oldpcd == newpcd){
alert("The pcdate has changed!");
return false;
}else document.submit();
}
</script>

thanks for looking at this.

Owl
30th September 2002, 02:13
Hi oberon, maybe
//-->
</script>
( •) (• )
>>V

oberon
30th September 2002, 04:02
thanks Owl
tried that it didn't work tho

OnlyTheTony
30th September 2002, 16:18
I'm no Coldfusion person (PHP personally) but...



<cfif etc...>
<td>bin.pcdate: #bin.pcdate#<br>
BIN.pcdate : BIN.pcdate
<!-- <cfset olddate = #BIN.pcdate#>-->
</td>
<script language="JavaScript">
<!--
var oldvar = "#BIN.pcdate#";
-->
</script>
</cfif>

..is that not it??

(that's how I'd do it in PHP)

oberon
2nd October 2002, 01:00
thanks OnlyTheTony but it didn't work :(
any other suggestions?

piglet
2nd October 2002, 09:42
Please can you post a copy of the generated javascript

If we can see the output code (not the code as it appears on the server) we should be able to help.

If you bring up the page on your browser and do a view->source that's what we want to see!

NightHawk
4th October 2002, 17:28
Couple of things....

1.) Did you use <cfoutput> when you were displaying the variable?


<Cfoutput>
<script language="JavaScript">
<!--
var oldvar = #BIN.pcdate#;
-->
</script>
</cfoutput>

2.) Once the page has loaded, you can not pass ColdFusion variables to JavaSript. Coldfusion is a server side technology and javascript is a client side technology. In order for coldfusion to manipulate the contents of a javascript variable, you must somehow get the javascript variable back to the server so that a new coldfusion page can execute the variable.

HTH!