CFLib.org – Common Function Library Project

QueryStringDeleteVar(variable [, qs])

Last updated February 24, 2002

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

 
Rated 3 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:

view plain print about
<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:

view plain print about
<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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Adam Cameron Adam Cameron added
composeDateTime
20 day(s) ago

Chris Weller Chris Weller added
convertQueryStri...
a while ago

Greg Nettles Greg Nettles added
arrayDiff
a while ago

Nathan Dintenfass Nathan Dintenfass added
ArrayOfStructsSo...
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 36 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Rachel Lehman deAccent
Rated 5.0, 6 time(s)

Isaac Dealey                                      countArbitraryDa...
Rated 5.0, 5 time(s)

Created by Raymond Camden / Design by Justin Johnson