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

Format JS var inside JS function with CF!

Participant ,
May 15, 2006 May 15, 2006

Copy link to clipboard

Copied

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...
TOPICS
Advanced techniques

Views

588

Translate

Translate

Report

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

LEGEND , May 17, 2006 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.

Votes

Translate

Translate
Enthusiast ,
May 15, 2006 May 15, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Participant ,
May 17, 2006 May 17, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Engaged ,
May 17, 2006 May 17, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
May 17, 2006 May 17, 2006

Copy link to clipboard

Copied

It's possible. Set your cf var before the html body tag and run your js function in the body onLoad event handler.

Votes

Translate

Translate

Report

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
Contributor ,
May 18, 2006 May 18, 2006

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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
Participant ,
May 22, 2006 May 22, 2006

Copy link to clipboard

Copied

LATEST
Dan
Thanks for the response - This worked a treat!

Votes

Translate

Translate

Report

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
Documentation