Skip to main content
Inspiring
May 15, 2006
Answered

Format JS var inside JS function with CF!

  • May 15, 2006
  • 4 replies
  • 804 views
OK - I'll try and explain:
I want to use CF (MX6) to format a js variable while inside a js function:

e.g:
<cfoutput>
<script language="JavaScript" type="text/javascript">
function formatSomething() {
var something = "#DateFormat('02/04/06','dd MMM yyyy')#";
alert(something);
}
</script>
</cfoutput>

Now this is ok if I hardcode the value of 'something'. What if I want to pass 'something' in?

<cfoutput>
<script language="JavaScript" type="text/javascript">
function formatSomething(something) {
var somethingElse = "#DateFormat('"+something+"','dd MMM yyyy')#";
alert(somethingElse);
}
</script>
</cfoutput>

Of course this doesn't work! - Neither does:
var somethingElse = eval("#DateFormat('"+something+"','dd MMM yyyy')#");
var somethingElse = "#Evaluate(DateFormat('"+something+"','dd MMM yyyy'))#";
var somethingElse = eval("#Evaluate(DateFormat('"+something+"','dd MMM yyyy'))#");

It's not specifically dates either - I just want to know if it's possible to do this someway?
I'm currently looking at opening a child window off screen to do what I want and pass back to the JS function but it seems like an urrggggghhh way to do it..

Ta for any help...
This topic has been closed for replies.
Correct answer Dan_Bracuk
It's possible. Set your cf var before the html body tag and run your js function in the body onLoad event handler.

4 replies

DJ_JambaAuthor
Inspiring
May 22, 2006
Dan
Thanks for the response - This worked a treat!
Inspiring
May 18, 2006
If I have this right, you have a form where a user enters data and then the function is called to convert the data to a different format? Or does the data exist already in the form and user calls function(button click, etc) to convert the data?
Dan_BracukCorrect answer
Inspiring
May 17, 2006
It's possible. Set your cf var before the html body tag and run your js function in the body onLoad event handler.

Inspiring
May 15, 2006
js is client side, cf is server side.

So, when cf tries to evaluate the "somethingElse" "something" does not exist.

But once the page is loaded into the browser, then you can use "something" using js code.

Ken
DJ_JambaAuthor
Inspiring
May 17, 2006
Thanks for the response.
I do understand the client/server logic but are you saying it's not possible to set a js var with coldfusion and return the var via the same script with 1 page load?
If this is the case - this surprises me with such a capable language. I'm no expert but if it's really not worth me experimenting this route I'll have to try 'somethingElse' ...


Thanks for your help
Inspiring
May 18, 2006
To do this you would have to send the variable back to the CF server Asychronously and then handle the response. You could do this via some of the AJAX libraries out there. Have a look at Prototype http://prototype.conio.net/ and also Joe Danziger's Blog http://www.ajaxcf.com/blog/index.cfm