Call variable javascript on coldfusion

I want to use CreateDate,datepart CF without submit (on javascript)

Page 1 of 1

3 Replies - 13539 Views - Last Post: 12 February 2009 - 12:53 PM Rate Topic: -----

#1 imamkomc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 09-May 07

Call variable javascript on coldfusion

Posted 11 February 2009 - 05:44 AM

Hi All,

Ihave a problem to call variable from javascript to coldfusion.
This my code :

..
<script>
var tgl_in=parseInt(document.forms[0].tgl_lahir1_1.value);
var bulan_in=parseInt(document.forms[0].tgl_lahir1_2.value)-1;
var tahun_in=parseInt(document.forms[0].tgl_lahir1_3.value);
var start_date=new Date(tahun_in,bulan_in,tgl_in); //bulan 1 - 11
var hari_ini=new Date();
var satu_hari=1000*60*60*24;
var var_hari=parseInt((hari_ini.getTime()-start_date.getTime())/(satu_hari));

alert("now() :" + '<cfoutput>#dateformat(now(),"dd mmm yyyy")#</cfoutput>');

// HOw to write this code.. ??
alert("fulltgl_lahir :" + '<cfoutput>#datepart('yyyy',CreateDate(tahun_in,bulan_in,tgl_in)#</cfoutput>'); 


</script>
..



Please give me solution..
Thank's a lot.

Is This A Good Question/Topic? 0
  • +

Replies To: Call variable javascript on coldfusion

#2 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Call variable javascript on coldfusion

Posted 11 February 2009 - 12:25 PM

View Postimamkomc, on 11 Feb, 2009 - 04:44 AM, said:

Hi All,

Ihave a problem to call variable from javascript to coldfusion.
....
// HOw to write this code.. ??
alert("fulltgl_lahir :" + '<cfoutput>#datepart('yyyy',CreateDate(tahun_in,bulan_in,tgl_in)#</cfoutput>');


I think you are confused about client versus server side code.

You cannot use ColdFusion code on javascript variables. ColdFusion code runs first, on the server. It is then converted to html/javascript and sent to your browser. Finally, the html/javascript code runs in your browser. Since javascript runs on the client side (ie browser), it doesn't know anything about ColdFusion functions, and vice versa.

So you have have to use javascript date functions instead.
http://www.w3schools...ef_obj_date.asp
Was This Post Helpful? 0
  • +
  • -

#3 imamkomc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 09-May 07

Re: Call variable javascript on coldfusion

Posted 12 February 2009 - 08:39 AM

Thank you for your replay :^: .
I was not to know that can not run the javascript code in coldfusion.
I far as one can use a javascript variable from coldfusion,
like
var var_java= '<cfoutput>#form.nilai#</cfoutput>'; 

So I think that there is a way to use javascript variable in coldfusion.
If from friends who have know how, please give to me..
Before I finally decided to use a full javascript code to build the code that I wake up. :D :D

Best Regard,
ImamkomC
Was This Post Helpful? 0
  • +
  • -

#7 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Call variable javascript on coldfusion

Posted 12 February 2009 - 12:53 PM

View Postimamkomc, on 12 Feb, 2009 - 07:39 AM, said:

So I think that there is a way to use javascript variable in coldfusion.


Unfortunately, no.

You can use ColdFusion to createjavascript code (like you have above). But ColdFusion does not know how to execute javascript. Nor can javascript execute ColdFusion code.

Consider how typical web pages operate.

1) You open a browser, and request a web page like: www.somecompany.com/products.cfm

2) The ColdFusion server parses the CF code for page "products.cfm", and converts it to html. That is all ColdFusion does. CF is not capable of executing any client side javascript code in that page.

Example: This ColdFusion code:

	<cfset form.nilai = "this value is from ColdFusion">
	<script>
		var var_java= '<cfoutput>#form.nilai#</cfoutput>';
	</script>
	


Is converted into this html code:

	<script>
		var var_java= 'this value is from ColdFusion';
	</script>
	 



3) The webserver then sends that html to your browser and closes the http connection. (ie So you are no longer connected to the server.)

4) Finally, your browser receives the html and displays it. Any javascript code in the page is then run in your browser (only).

By the time your browser executes the javascript, you are no longer connected to the server. Since ColdFusion code can _only_ run on the server - there is no way to execute CF code from your browser (ie client side javascript). Not without sending another http request. Make sense?
Was This Post Helpful? 1

Page 1 of 1