jQuery form input defaultValue

When creating a form to accept user info, there are times when functions related to a form input's default value are required. For example, you have a field that says "search here", and when the user clicks in the field, the default value is removed so the user can begin typing. But, if the user leaves the field blank, you might want that default term to return. In regular javascript, we use something like this:


if(inputName.value == ''){
inputName.value = inputName.defaultValue
};

but jQuery doesn't have a built-in method for retrieving that default value. Thankfully, google found me a super easy workaround - this discussion thread holds the key:


if ($(this).val() == "") {
var defVal = $(this)[0].defaultValue;
$(this).val(defVal);
}

Note the [0] in the defaultValue. As explained in the thread linked above: "If you imagine $(this) as an array of DOM objects within jQuery, then $(this)[0] is a way to access the first (or in this case, only) DOM object. jQuery doesn't have a built in way to retrieve the default value of an input, so we have to nip back into the DOM momentarily in order to retrieve it. "

Add Leading Zeros to a Numeric Value with ColdFusion

ColdFusion's numberformat() allows for the use of '_' (underscore) or '9' as a placeholder in a numeric mask. But these do not add a leading zero to non-decimal values. ( If the number contains fewer digits than the mask, you get the truncated number without any zeros on the left side).

In some cases, however, you want those zeros. The super-simple CF solution? Use a '0' as the mask character!


<cfset theNumber = "1287">
<cfoutput>#NumberFormat(TheNumber, "000009")#</cfoutput>

This returns 001287


<cfset theNumber = "1287">
<cfoutput>#NumberFormat(TheNumber, "999999")#</cfoutput>

This returns 1287

View XML directly in Firefox without the annoying 'add to reader' screen

This is one of those little things that has bugged me for quite some time, but not quite enough to do anything about... until now... 

Whenever I try to view an xml file, whether it be a sitemap.xml file from one of my website projects, a data export file, or an rss feed, Firefox shows me a screen with two options, "add to google reader" or "add to google homepage" ... usually I want neither, I just want to see my xml!

The fix was actually super simple... but, lest I forget at some point ( most likely not too far in the future) how I managed to banish this little annoyance, I'll post it here:

1) In FF, go to Tools > Options in the top menu
2) Select the Applications tab
3) Scroll down to "Web Feed" (how annoying ... not 'xml', not 'rss', but 'web feed'? really? )
4) Change the Web Feed 'action' to "Preview in Firefox" (mine said "Use Google" before changing)
5) Click the 'ok' button and go to any .xml file url to see all the wonderful XML , sans reader-prompt annoyance !

 

121 Blackberry Tips - some good ones, too!

A friend and fellow Blackberry enthusiast just turned me on to this blog post featuring a very  comprehensive list of BB tips and resources, including several very useful keyboard shortcuts and other tricks I was not aware of at all.

Here's the full link: http://www.insidecrm.com/features/121-blackberry-tips-021408/

Get the recordcount of a ColdFusion update, insert or delete query using CFquery result attribute

When you run a cfquery using SELECT, the number of records returned is easy to find using #queryname.recordcount# - but what about the other types of SQL queries, our good friends Update, Insert and Delete ? Those query types don't return a total of rows in the same way.

But there's an easy solution ... once again, we can use 'recordcount', but in this case it is a node of the 'result' variable as defined in our cfquery tag.



<cfquery datasource="#request.dsn#" name="saveChanges" result="updateResult">
UPDATE table_name
SET
column1 = '#value1#',
column2 = '#value2#'
WHERE column3 = 1
</cfquery>

<cfset recordsChanged = updateResult.recordCount>

That query is just a sample, you could have any 'where' statement, values, etc... but notice the "result" attribute of the cfquery tag, which will accept any variable name you want to use. Then, immediately following the query code, we simply get the 'recordcount' node of the result structure, and we have the number of updated records from the query.

( Note: this example is for mySQL - ymmv with other db types)

Get the ID of a newly created record from a ColdFusion insert query using CFquery result attribute

When inserting a new row to a mySQL database table using <cfquery>, it is often necessary to find the ID of the new record so you can refer to it in subsequent queries or other code further down the page.

There are a few ways to do this, but I like this one best:



<cfquery name="insertRecord" datasource="#request.dsn#" result="insertResult">
INSERT INTO my_table
(
column_1,
column_2,
etc
)
VALUES
(
'#value1#',
'#value2#',
'etc'
)
</cfquery>

<cfset newID = insertResult.generated_Key>

Notice the "result" attribute of the cfquery tag, which will accept any variable name you want to use. Then, immediately following the query code, we simple get the 'generated_Key' node of the result value, and voila - we have the ID of the new record, ready for whatever is needed next.

(Note: This example is for mySQL - not sure how this plays out in MS SQL or other DB engines... your mileage may vary.)

Verify remote images exist - prevent missing images with CFHTTP

I am creating a photo gallery where the images actually reside on a third-party service. To prevent 'blank' or missing images from showing up in my html gallery markup, I needed a way to verify the remote image actually exists before including it into the dynamic markup.

This seems to work quite nicely:



<cfoutput query="myImageQuery">

<cfset imgURL = "http://remotesite.com/images/#imageFilename#">
<!--- check for the image via cfhttp:
Note:'head' method gets only headers, which is all we need (thanks @ScottP) --->

<cfhttp url="#imgURL#" method="head">    
<!--- isolate the 'mimeType' value from the cfhttp results --->
<cfset myStatus = cfhttp.mimeType>
<!--- if the mimeType is jpg (jpeg), include the image in our output --->
<cfif myStatus eq "image/jpeg">
<img src="#imgURL#" alt="#imageTitle#">
</cfif>

</cfoutput>

Add alternate pricing option to Cartweaver

A Cartweaver user asked how he can add alternate pricing to his Cartweaver ecommerce site. He was understandably concerned about the need to edit the database, and how much code editing might be involved. But if you take a systematic approach, it is pretty straightforward.

The first part of setting up any alternate pricing system is to get that info into your database and admin, so you can work with it. In this case, since the basic goal is to duplicate the existing 'price' system, that is exactly what we are going to do.

more...

Marking current menu link with jQuery for CartWeaver categories

Working on a heavily-modified Cartweaver Coldfusion E-Commerce shopping cart site, I have created a database-driven navigation menu containing links to each category. When a user clicks a link, the products in that category are displayed, and the menu link for that category is highlighted via a special 'currentLink' css class.

Now of course I could use CF to add ' class="currentLink" ' to the current category's menu option, but I am not doing this particular menu that way. Besides, it is just as easy with jQuery!



<cfif cgi.script_name contains 'results.cfm' AND isDefined('url.category') and url.category gt 0>
<cfsavecontent variable="highlight">
<script type="text/javascript">
$(document).ready(function(){
$('.sideNav li a').removeClass('currentLink');
$('.sideNav li a[href=results.cfm?category=<cfoutput>#url.category#</cfoutput>]').addClass('currentLink');
//end jQuery
});
</script>
</cfsavecontent>
<cfhtmlhead text="#highlight#">
</cfif>

Simple enough - if we are on the 'results' page, and a category is defined in the url, remove the 'currentLink' class from all existing menu items, find the one where the 'href' attribute matches the current pagename and category, and add the class back to that link. Done.

The use of cfsavecontent and cfhtmlhead means I can put this right below the code that calls in my custom nav tag, for easy fine-tuning right where i need it (rather than in an external scripts file).

Quick , easy, and done in a flash with jQuery!

Redirect secure 'https' requests using getPageContext() and isSecure()

Working on a site using a shared ssl, where only 2 specific pages of the site are required to operate under the https:// SSL prefix, I needed a quick way to check whether a page request for either of those pages was using the 'https' prefix - if not, redirect, adding the 's'. Likewise, all other pages, we want to make sure they are not using the 's' and redirect without it.

I found this blog post, which got me started: http://www.chapter31.com/2007/07/21/detecting-and-redirecting-http-to-https/

Using the fun and interesting CF goodies
getPageContext() and isSecure()

<!--- REDIRECTION TO SSL / non-SSL --->
<cfset secureRequest = getPageContext().getRequest() />
<!--- for these pages, redirect to secure (https) page  --->
<cfif cgi.SCRIPT_NAME contains 'payment.cfm' OR cgi.SCRIPT_NAME contains 'payment_response.cfm'>
<cfif NOT secureRequest.isSecure()>
<cflocation url="https://#secureRequest.getServerName()##secureRequest.getRequestURI()#?#secureRequest.getQueryString()#" addtoken="false" />
</cfif>
<cfelse>
<!--- for all other pages, redirect to non-secure (http) page --->
<cfif secureRequest.isSecure()>
<cflocation url="http://#secureRequest.getServerName()##secureRequest.getRequestURI()#?#secureRequest.getQueryString()#" addtoken="false" />
</cfif>
</cfif>

Quick and simple.

Live HTTP Headers Firefox Extension

As part of my url-redirection efforts related to the launch of the new company site, I've been looking more closely at 301 redirection using ColdFusion ( see this post about CF 301 with CFlocation - it really is that simple! )

But, being somewhat skeptical that it could really, truly be that simple, I still wanted to verify that the redirection was going through as '301' status in my web browser.

On the advice of a friend, I downloaded the quick easy FireFox extension called "Live HTTP Headers" http://livehttpheaders.mozdev.org/

Within a few seconds, I had my proof. I did a quick google search that I knew would turn up my old site, clicked "Tools > Live Http Headers" in Firefox, and clicked on the first result for 'www.mredesign.com'.

Sure enough I was looking at the new site, www.gowestweb.com , and in the little Live Http popup, I had my 301 proof.

Here's part of the detailed output:

http://www.mredesign.com/

GET / HTTP/1.1 Host: www.mredesign.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://www.google.com/search?q=utah+web+designer&btnG=Search&hl=en&client=firefox-a

HTTP/1.x 301 Moved Permanently Connection: close Date: Wed, 12 Aug 2009 15:44:48 GMT Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET Location: http://www.gowestwebdesign.com/index.cfm Content-Type: text/html; charset=UTF-8 ---------------------------------------------------------- http://www.gowestwebdesign.com/index.cfm

GET /index.cfm HTTP/1.1 Host: www.gowestwebdesign.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://www.google.com/search?q=utah+web+designer&btnG=Search&hl=en&client=firefox-a

I am a happy (and safely redirected) camper. Very nice tool, works just like it should... and free!

More Entries

blogcfc 5.9.1.002 by raymond camden
contact michael evangelista