Skip to main content
Known Participant
July 13, 2010
Answered

Pass ColdFusion variable to the JavaScript window

  • July 13, 2010
  • 2 replies
  • 22996 views

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>

    This topic has been closed for replies.
    Correct answer DragonForest

    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>

    2 replies

    Inspiring
    July 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.

    nikoo560Author
    Known Participant
    July 13, 2010
    I tried everything it's not working, Please answer if you already tried it yourself before. I still need help.
    Thanks.
    nikoo560Author
    Known Participant
    July 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>

    ilssac
    Inspiring
    July 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.

    nikoo560Author
    Known Participant
    July 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.

    ilssac
    Inspiring
    July 13, 2010

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

    Can you share the exact error?