House of Fusion
Home of the ColdFusion Community

Search cf-talk

December 02, 2008

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
   1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31       

Search over 2,500 ColdFusion resources here  >>>      
Home /  Groups /  ColdFusion Talk (CF-Talk)

Javascript in CFC

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
chn g
09/04/2008 02:10 PM

What's wrong with the following code? <cfif testqry.recordcount EQ 1>   ....... <cfelse>    <script type="text/javascript">            Alert("Error");    </script>            <cflocation url="http://#CGI.SERVER_NAME#/testfile.html"> </cfif> It's not showing up alert box, directly displaying the html file. Thanks Chn

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Justin Scott
09/04/2008 02:14 PM

>    <script type="text/javascript"> >            Alert("Error"); >    </script>         >    <cflocation url="http://#CGI.SERVER_NAME#/testfile.html"> First thing is that CFLOCATION sets a 302 header which tells the browser to redirect, and none of the "output" is sent to the browser. If you want to show an error and redirect, you'll need to do it all in JavaScript (also, alert should be all lower-case)... alert("Error"); document.location.href = '/testfile.html'; -- Justin Scott, http://www.tlson.com/

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andy Matthews
09/04/2008 02:17 PM

CF runs on the server, before the Javascrtipt would even have a chance to hit the browser. What's wrong with the following code? <cfif testqry.recordcount EQ 1>   ....... <cfelse>    <script type="text/javascript">            Alert("Error");    </script>            <cflocation url="http://#CGI.SERVER_NAME#/testfile.html"> </cfif> It's not showing up alert box, directly displaying the html file. Thanks Chn

Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
brad
09/04/2008 02:14 PM

alert() is lower case.  If that doesn't work try this: The cflocation is sending back a status code of 301 to your browser.  In fact, I don't even know if ColdFusion sends back any content created in the reponse body or not. ~Brad What's wrong with the following code?

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Justin Scott
09/04/2008 02:19 PM

> The cflocation is sending back a status code of 301 to your browser. You mean it sets 302 as a temporary redirect, not permanent as a 301 would be. > In fact, I don't even know if ColdFusion sends back any content > created in the reponse body or not. It doesn't when you use CFLOCATION.  It's essentially the equivalent of setting a 302 status code, a Location header, then using CFCONTENT with the reset="yes" attribute. -- Justin Scott, http://www.tlson.com/

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Milburn, Steve
09/04/2008 02:22 PM

This works?  Am I missing something?  How is the browser seeing javascript code in a cfc? ________________________________________ From: chn g [rchandra@empoint.com] Sent: Thursday, September 04, 2008 2:14 PM To: CF-Talk Subject: Re: Javascript in CFC Thank you, its working now. ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Justin Scott
09/04/2008 02:28 PM

> This works?  Am I missing something?  How is the browser seeing javascript code in a cfc? The CFCOMPONENT and CFFUNCTION tags both support an "output" attribute that can be set to yes to allow their output to be injected into the content stream back to the user. -- -Justin Scott, http://www.tlson.com/

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andy Matthews
09/04/2008 02:34 PM

That's irrelevant. The ColdFusion code would completely process before ANY javascript code would even make it to the browser. > This works?  Am I missing something?  How is the browser seeing javascript code in a cfc? The CFCOMPONENT and CFFUNCTION tags both support an "output" attribute that can be set to yes to allow their output to be injected into the content stream back to the user. -- -Justin Scott, http://www.tlson.com/

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Justin Scott
09/04/2008 02:54 PM

Andy Matthews wrote: > That's irrelevant. > > The ColdFusion code would completely process before ANY javascript code > would even make it to the browser. Actually, you can have output go to the browser before the CF code completes by using CFFLUSH.  This would negate CFLOCATION though, so the original poster probably removed that in favor of another method of redirection (such as having it all in JavaScript with a CFABORT to stop further processing after the error was detected).  In any case, they didn't say how it was solved, but what matters is that whatever they did worked for them <g>. -- Justin Scott, http://www.tlson.com/

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
chn g
09/04/2008 03:22 PM

I use document.location.href in javascript instead of cflocation. ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Aaron Rouse
09/04/2008 03:20 PM

I have some CFCs I use to build needed javascript/html that is outputted onto pages at the time they are loaded. On Thu, Sep 4, 2008 at 1:29 PM, Andy Matthews <lists@commadelimited.com>wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Brad Wood
09/04/2008 11:11 PM

> That's irrelevant. > > The ColdFusion code would completely process before ANY JavaScript code > would even make it to the browser. Andy, I don't think you understand how cflocation works. Even though it is a CF tag, the relocation does NOT happen on the server side.  If you make a test file and place the following code in it: <script LANGUAGE="JavaScript" TYPE="text/JavaScript">     alert('test'); </script> <cflocation url="http://www.yahoo.com"> ... and then hit that page, your browser will make two requests.  The first request for your test page will come back looking much like this HTTP response: ========================================= HTTP/1.1 302 Moved Temporarily Date: Fri, 05 Sep 2008 03:04:33 GMT Server: Apache/2.2.4 (Win32) JRun/4.0 location: http://www.yahoo.com Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/html; charset=UTF-8 Content-Length: 6551 <script LANGUAGE="JavaScript" TYPE="text/javascript">     alert('test'); </script> ========================================= Notice that the output of your page (the JavaScript) DOES get passed out to the browser.  However, your browser (Internet Explorer at least) will ignore the body of the response and send out a second request for http://www.yahoo.com. So whether or not the code is in a CFC or not, the browser is who actually does the redirect.  Of course, if one wants the JavaScript to execute BEFORE the redirect, you want to send back a 200 response like so: <script LANGUAGE="JavaScript" TYPE="text/JavaScript">     alert('test');     document.location.href = 'http://www.yahoo.com'; </script> ~Brad

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andy Matthews
09/05/2008 09:39 AM

Hrm... You're right. That doesn't make any sense though. > That's irrelevant. > > The ColdFusion code would completely process before ANY JavaScript code > would even make it to the browser. Andy, I don't think you understand how cflocation works. Even though it is a CF tag, the relocation does NOT happen on the server side.  If you make a test file and place the following code in it: <script LANGUAGE="JavaScript" TYPE="text/JavaScript">     alert('test'); </script> <cflocation url="http://www.yahoo.com"> .... and then hit that page, your browser will make two requests.  The first request for your test page will come back looking much like this HTTP response: ========================================= HTTP/1.1 302 Moved Temporarily Date: Fri, 05 Sep 2008 03:04:33 GMT Server: Apache/2.2.4 (Win32) JRun/4.0 location: http://www.yahoo.com Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/html; charset=UTF-8 Content-Length: 6551 <script LANGUAGE="JavaScript" TYPE="text/javascript">     alert('test'); </script> ========================================= Notice that the output of your page (the JavaScript) DOES get passed out to the browser.  However, your browser (Internet Explorer at least) will ignore the body of the response and send out a second request for http://www.yahoo.com. So whether or not the code is in a CFC or not, the browser is who actually does the redirect.  Of course, if one wants the JavaScript to execute BEFORE the redirect, you want to send back a 200 response like so: <script LANGUAGE="JavaScript" TYPE="text/JavaScript">     alert('test');     document.location.href = 'http://www.yahoo.com'; </script> ~Brad


<< Previous Thread Today's Threads Next Thread >>

Mailing Lists