Copy link to clipboard
Copied
Hi everyone,
I have a java script window that is will open when user click on the link, my problem is that how I can pass a ColdFusion variable to the window. I am using ColdFusion 7 so I can not use cfwindow.what shold I put on the *** place??? I tried to put EntityId in the call place javascript:poponload(EntityId ); and function poponload(EntityId ), doesn’t 't work either so please let me know if you know how to do this.
My code:
<SCRIPT language="javascript">
function poponload()
{
window.open ('test.cfm?test='*** ,'mywindow','menubar=1,resizable=1,width=350,height=250');
}
</SCRIPT>
<body>
<cfform>
<cfset EntityId ="#form.EntityId#"> /I want to pass EntityId to the javascript window
</cfif>
<a href="javascript:poponload();">Find IDs</a>
</cfform>
</body>
Many thanks, Rob - Apropos to this topic, do you (or does anybody) know what I'm doing wrong with the code below? (the message in the "alert" box is #x#, so the function isn't picking up the Coldfusion variable, x). Many thanks in advance!
<head>
<cfset x=3>
<script language="javascript">
function myFunc() {
alert("#x#");
}
</script>
</head>
<body>
<script language="javascript">
myFunc();
</script>
</body>
Copy link to clipboard
Copied
You can't "PASS" a ColdFusion variable to JavaScript. At least not like most novice ColdFusion developers mean when they think of passing variables.
CFML and JavaScript are two completely different programs that will run on two completely different computers with two completely different variable memories. In other words the two can never meet.
Now, looking at your code. it looks like you would just like to output a ColdFusion variable into the JavaScript code. You do that exactly the same way you output a variable into HTML code to be sent to the browser.
<cfoutput>
<SCRIPT language="javascript">
function poponload()
{
window.open ('test.cfm?test='#form.EntityID#' ,'mywindow','menubar=1,resizable=1,width=350,height=250');
}
</SCRIPT>
</cfoutput>
ColdFusion does not care if it is creating HTML, JavaScript, CSS, or any other text to be sent to the client that requested it. And it can be a very powerful tool to create dynamic JS. Just keep it clearly in mind that the JavaScript and the CFML are not coexisting in any way.
Just to get you a head start, if you want to use JavaScript to send data to ColdFusion (usually the next evolutionary step) you will be looking at frames, ajax or screen refresh to have JavaScript create a new request to the web server.
Copy link to clipboard
Copied
I tried this one and gave an error invalid character probably for #
Also I need to pass the value of #form.EntityID# not as a string.
Copy link to clipboard
Copied
You put a <cfoutput>....</cfoutput> block around the <script>...</script> block, correct.
Can you share the exact error?
Copy link to clipboard
Copied
Variable ENTITYID is undefined. I have this too by the way:
<cfparam name="form.EntityId" default="">
<cfif isdefined ("form.EntityId")>
<cfset EntityId ="#form.EntityId#">
</cfif>
Copy link to clipboard
Copied
I asume you also knew enough to remove the extra single quote that was errantly put into my sample code!
<cfoutput>
<SCRIPT language="javascript">
function poponload()
{
window.open ('test.cfm?test=#form.EntityID#' ,'mywindow','menubar=1,resizable=1,width=350,height=250');
}
</SCRIPT>
</cfoutput>
This may also need some type of test around it so that this piece of code does not run if a form has not yet been submitted so that form.entity does not exist. Either that, or default some value for the form.entityid variable.
Copy link to clipboard
Copied
Ilssac - not sure why you say this - but it is absolutely not correct. Coldfusion is a serverside application code and can pass coldfusion based variables into Javascript code which will be executed on the client side like all the other HTML related code.
Take
<cfset somevar = "##FF0000">
<cfoutput>
<script type="text/javascript">
$(function () {Highcharts.setOptions({
chart: {backgroundColor: {#somevar#....
This way you take your CF based content as a variable and pass it on to a javascript that runs on the client.
Copy link to clipboard
Copied
Many thanks, Rob - Apropos to this topic, do you (or does anybody) know what I'm doing wrong with the code below? (the message in the "alert" box is #x#, so the function isn't picking up the Coldfusion variable, x). Many thanks in advance!
<head>
<cfset x=3>
<script language="javascript">
function myFunc() {
alert("#x#");
}
</script>
</head>
<body>
<script language="javascript">
myFunc();
</script>
</body>
Copy link to clipboard
Copied
OK . . . I figured it out as below. Needed <cfoutput> to scope the variable, then needed to pass the variable as an argument to the function.
<script language="javascript">
function myFunc(x) {
alert(x);
}
</script>
<cfoutput>
<cfset x=3>
<script language="javascript">
myFunc("#x#");
</script>
</cfoutput>
Copy link to clipboard
Copied
Rewrite your poponload function so that it accepts an input parameter for the value of test. Then, in your link, you can put a coldfusion variable inside the brackets when you call the function.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Ok , I found out myself , it was easier than I thought:
<SCRIPT language="javascript">
function poponload()
{
var Entity = document.getElementById('EntityId').value;
window.open ("test.cfm?EntityId="+Entity);
}
</SCRIPT>