Wednesday, December 2, 2009

Javascript and ColdFusion

JavaScript changed my url variables.

I recently debugged a piece of old code that was doing some weirdness. In the code a JavaScript function was run after a combo box was changed. The function reloaded the same page with a few url variables. One of the url variables was changing when it reloaded. I found out JavaScript was calculating the numerical value and returning a different number, ( example: 027 became 17 ).

The original code looked something like this:
<code>
function recalcWeek()
{
var year=document.forms[0].slctFiscalYear.options[document.forms[0].slctFiscalYear.selectedIndex].value;
location.href="somePage.cfm?div_no="+<cfoutput>#url.div_no#</cfoutput>+"&event_id="+<cfoutput>"#url.Event_ID#"</cfoutput>+"&fiscal_year="+year;
}
</code>

The corrected code to fix the issue looked like this:
<code>
function recalcWeek()
{
var year=document.forms[0].slctFiscalYear.options[document.forms[0].slctFiscalYear.selectedIndex].value;
location.href="somePage.cfm?div_no=<cfoutput>#url.div_no#</cfoutput>&event_id=<cfoutput>#url.Event_ID#</cfoutput>&fiscal_year="+year;
}
</code>


The mistake was using the double quotes and the + to try and concatenate the values. ColdFusion will render the variable in the ## before JavaScript uses them so there is no need to concatenate. If you would do a view source on the page, or use Firebug, you would see the function after ColdFusion renders the variables and then the mistake becomes more obvious.

Wednesday, June 24, 2009

How to display HTML code in a browser.

How can I show HTML code without the browser rendering it?

Replace '&' with '&amp;'
Replace '<' with '&lt;'
Replace '>' with '&gt;'

See the following link for more detailed info:
http://allmyfaqs.net/faq.pl?Show_HTML_examples

Go back in browser using Javascript.

<input type="Button" name="btnCancel" value="Cancel" onclick="history.go(-1);">

Loop through form fields in Javascript

The following code will loop through all form fields and look for ones named "chkStores" and checks or unchecks them (assuming they are check boxes). There is a hidden form field named "StoreCheck" that tells whether the current state is checked or unchecked. The action becomes the reverse of the "StoreCheck" value


function check_all()
{
var chkStatus = eval("document.forms[0].StoreCheck.value");
var form = document.forms[0];
var theName = "chkStores";
var len = form.elements.length;
for (var i = 0; i < len; i++)
{
var e = form.elements[i];
if (e.name == theName)
{
if (chkStatus == "unchecked")
{
e.checked = true;
eval("document.forms[0].StoreCheck.value = 'checked'");
}
else
{
e.checked = false;
eval("document.forms[0].StoreCheck.value = 'unchecked'");
}
}
}
}


This code displays the checkboxes and a clickable span to check all.


<code>
<span style="text-decoration: underline;" onmouseover="this.style.cursor='hand';return overlib('Click Here To Check/Uncheck All Stores');" onmouseout="return nd();" onclick="javascript:check_all()">Stores
</span>
<input type="Hidden" name="StoreCheck" value="unchecked">

<cfoutput query="qryGetActiveStores">
<input type="checkbox" name="chkStores" value="#trim(qryGetActiveStores.Store)#">#qryGetActiveStores.Store#
</cfoutput>
</code>

Friday, March 6, 2009

CGI Variables

We recently migrated an old app from an IIS web server to an Apache web server. During this migration we found out the the code had referenced CGI.path_name which did not work in Apache. The CGI variable was blank. We changed the CGI varible to be script_name and all worked fine. I also found a couple of good blog posts about CGI variables and some gotcha's:

CGI.hot_n_sexy Does Not Throw A ColdFusion Error

Apache 404 / CGI Weirdness....