In ColdFusion, how can I determine if a variable exists within the querystring without throwing an error attempting to check it?
There are two options.
The first is to use cfparam to define a default value eg:
<cfparam name="url.varname" type="string" default="" />
This ensures that you can always refer to url.varname
The second is to use isDefined or structKeyExists to test for the presence of the variable:
<cfif isDefined("url.varname") and url.varname eq 42> do something </cfif>
<cfif structKeyExists(url, "varname") and url.varname eq 42> do something </cfif>
I have used this approach in many places.
At the top of the page:
<cfparam name="request.someVal" default="request.defaultVal">
Later in the page or custom tag, check for the value of the request.someVal variable, without fear of it crashing, since it has a default value.
<cfif ("request.someVal" eq "something")> ... </cfif> . . .
<cfscript>, you can
param url.varname; // throws error if it does not exist param url.varname = ""; // sets value it was not already set