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, December 2, 2009
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 '
Replace '<' with '
Replace '>' with '
See the following link for more detailed info:
http://allmyfaqs.net/faq.pl?Show_HTML_examples
Replace '&' with '
&
'Replace '<' with '
<
'Replace '>' with '
>
'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
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>
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....
CGI.hot_n_sexy Does Not Throw A ColdFusion Error
Apache 404 / CGI Weirdness....
Thursday, December 18, 2008
Test for radio button value in JavaScript
If you would like to test which radio button is selected in JavaScript you must remember that each button always has a value but only one will have a checked value in the same button group. So to find the selected one you must loop through the group and test the checked value ( see function getSelectedRadioButton() ).
function getSelectedRadioButton(buttonGroup)
{
for ( var i = 0; i < buttonGroup.length; i++)
{
if (buttonGroup[i].checked)
{
return buttonGroup[i].value;
}
}
return 0
}
The function above will return the value assigned to the checked radio button, not the positional value i that it is inside the button group.
Function fnAlertRadioButtonChecked() below will display an alert telling you which radio button was selected. This function can be called from an event in the form.
function fnAlertRadioButtonChecked(form)
{
var j = getSelectedRadioButton(form.myButtonGroupName);
if (j == 'first')
{
alert('The radio button assigned a value of first is checked.');
}
else if (j == 'second')
{
alert('The radio button assigned a value of second is checked.');
}
else if (j == 'third')
{
alert('The radio button assigned a value of third is checked.');
}
else
{
alert('The radio button assigned a value of ' & j & ' is checked.');
}
}
function getSelectedRadioButton(buttonGroup)
{
for ( var i = 0; i < buttonGroup.length; i++)
{
if (buttonGroup[i].checked)
{
return buttonGroup[i].value;
}
}
return 0
}
The function above will return the value assigned to the checked radio button, not the positional value i that it is inside the button group.
Function fnAlertRadioButtonChecked() below will display an alert telling you which radio button was selected. This function can be called from an event in the form.
function fnAlertRadioButtonChecked(form)
{
var j = getSelectedRadioButton(form.myButtonGroupName);
if (j == 'first')
{
alert('The radio button assigned a value of first is checked.');
}
else if (j == 'second')
{
alert('The radio button assigned a value of second is checked.');
}
else if (j == 'third')
{
alert('The radio button assigned a value of third is checked.');
}
else
{
alert('The radio button assigned a value of ' & j & ' is checked.');
}
}
Thursday, September 11, 2008
MXUnit
I setup MXUnit and the mxunit eclipse plugins on my local development environment. Tried it out on a couple of CFCs I had built for a project at work. I went through 3 round of tests on one of the CFCs and "walla"!! I found an error. I am sold on testing and MXUnit now. It is really cool to be able to just run these test, each and every time I modify the CFC.
A side note that may or may not effect very many people is a compatibility issue with the software I have installed. My employer decided to use Altiris to manage the migration of Lotus Notes to Outlook and office 2007. Altiris has issues with Eclipse, CFEclipse, Fusebox, MXUnit, CodeCop, etc... Certain XML file parsing will cause our systems to blue screen. The BSOD is usually when you browse the sites that parse certain XML files. Our company is actively troubleshooting this issue, no resolution yet. Fusebox needed a couple of XML files modified. CodeCop has not been fixed yet, BSOD every time.
MXUnit also caused a BSOD when browsing the MXUnit directory. With some help from Adam Haskell at work, there were two XML files found to cause BSOD (mxunit\framework\mxunit-config.xml and mxunit\doc\api\config\config.xml). All we needed to do to stop the BSOD was to remove the comments out of the XML files. This is probably only a bandaid so hopefully we will find the real solution to make Altiris play nice with our setup.
A side note that may or may not effect very many people is a compatibility issue with the software I have installed. My employer decided to use Altiris to manage the migration of Lotus Notes to Outlook and office 2007. Altiris has issues with Eclipse, CFEclipse, Fusebox, MXUnit, CodeCop, etc... Certain XML file parsing will cause our systems to blue screen. The BSOD is usually when you browse the sites that parse certain XML files. Our company is actively troubleshooting this issue, no resolution yet. Fusebox needed a couple of XML files modified. CodeCop has not been fixed yet, BSOD every time.
MXUnit also caused a BSOD when browsing the MXUnit directory. With some help from Adam Haskell at work, there were two XML files found to cause BSOD (mxunit\framework\mxunit-config.xml and mxunit\doc\api\config\config.xml). All we needed to do to stop the BSOD was to remove the comments out of the XML files. This is probably only a bandaid so hopefully we will find the real solution to make Altiris play nice with our setup.
Subscribe to:
Posts (Atom)