CFLib.org – Common Function Library Project

QueryStringDeleteVar(variable [, qs])

Last updated February 24, 2002
Download UDF

author

Nathan Dintenfass                                 Nathan Dintenfass

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 2 time(s). Average Rating: 5.0

Description:
Delete a variable (or a set of variables) and its value from a query string. By default, it uses the cgi.query_string, but you can pass in an optional second argument to replace the query string. This function is handy when you need to preserve an entire query string, but you want to kill off one of the variables. For example, if you have a button that appends "logout=yes" to the existing URL and you want to preserve that URL but no longer have the logout variable.

Return Values:
Returns a string.

Example:

<cfset qs = "arg1=1&arg2=2&arg3=3&arg4=4">
<cfoutput>
#queryStringDeleteVar("arg1",qs)#<br>
#queryStringDeleteVar("arg1,arg4",qs)#<br>
</cfoutput>

Parameters:

Name Description Required
variable A variable, or a list of variables, to delete from the query string. Yes
qs Query string to modify. Defaults to CGI.QUERY_STRING. No

Full UDF Source:

<cfscript>
/**
* Deletes a var from a query string.
* Idea for multiple args from Michael Stephenson (michael.stephenson@adtran.com)
*
* @param variable      A variable, or a list of variables, to delete from the query string.
* @param qs      Query string to modify. Defaults to CGI.QUERY_STRING.
* @return Returns a string.
* @author Nathan Dintenfass (michael.stephenson@adtran.comnathan@changemedia.com)
* @version 1.1, February 24, 2002
*/

function queryStringDeleteVar(variable){
    //var to hold the final string
    var string = "";
    //vars for use in the loop, so we don't have to evaluate lists and arrays more than once
    var ii = 1;
    var thisVar = "";
    var thisIndex = "";
    var array = "";
    //if there is a second argument, use that as the query string, otherwise default to cgi.query_string
    var qs = cgi.query_string;
    if(arrayLen(arguments) GT 1)
        qs = arguments[2];
    //put the query string into an array for easier looping
    array = listToArray(qs,"&");        
    //now, loop over the array and rebuild the string
    for(ii = 1; ii lte arrayLen(array); ii = ii + 1){
        thisIndex = array[ii];
        thisVar = listFirst(thisIndex,"=");
        //if this is the var, edit it to the value, otherwise, just append
        if(not listFind(variable,thisVar))
            string = listAppend(string,thisIndex,"&");
    }
    //return the string
    return string;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson