|
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Javascript in CFC
What's wrong with the following code?chn g 09/04/08 02:10 P > <script type="text/javascript">Justin Scott 09/04/08 02:14 P CF runs on the server, before the Javascrtipt would even have a chance toAndy Matthews 09/04/08 02:17 P alert() is lower case. If that doesn't work try this:brad 09/04/08 02:14 P > The cflocation is sending back a status code of 301 to your browser.Justin Scott 09/04/08 02:19 P Thank you, its working now.chn g 09/04/08 02:19 P This works? Am I missing something? How is the browser seeing javascript code in a cfc?Milburn, Steve 09/04/08 02:22 P > This works? Am I missing something? How is the browser seeing javascript code in a cfc?Justin Scott 09/04/08 02:28 P That's irrelevant.Andy Matthews 09/04/08 02:34 P Andy Matthews wrote:Justin Scott 09/04/08 02:54 P I use document.location.href in javascript instead of cflocation.chn g 09/04/08 03:22 P I have some CFCs I use to build needed javascript/html that is outputtedAaron Rouse 09/04/08 03:20 P > That's irrelevant.Brad Wood 09/04/08 11:11 P Hrm...Andy Matthews 09/05/08 09:39 A 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 > <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/ 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 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? > 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/ Thank you, its working now. ----- Excess quoted text cut - see Original Post for more ----- 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 ----- > 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/ 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/ 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/ I use document.location.href in javascript instead of cflocation. ----- Excess quoted text cut - see Original Post for more ----- 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 ----- > 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 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
|
Mailing Lists
|
Latest Fusion Authority Articles
|
||||||