Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Pass ColdFusion variable to the JavaScript window

New Here ,
Jul 13, 2010 Jul 13, 2010

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>

22.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

New Here , May 29, 2014 May 29, 2014

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>

Translate
Valorous Hero ,
Jul 13, 2010 Jul 13, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 13, 2010 Jul 13, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jul 13, 2010 Jul 13, 2010

You put a <cfoutput>....</cfoutput> block around the <script>...</script> block, correct.

Can you share the exact error?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 13, 2010 Jul 13, 2010

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jul 13, 2010 Jul 13, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 20, 2013 Nov 20, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 29, 2014 May 29, 2014

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 29, 2014 May 29, 2014
LATEST

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 13, 2010 Jul 13, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 13, 2010 Jul 13, 2010
I tried everything it's not working, Please answer if you already tried it yourself before. I still need help.
Thanks.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 13, 2010 Jul 13, 2010

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources