Showing posts with label url. Show all posts
Showing posts with label url. Show all posts

Sunday, October 23, 2011

JavaScript: Get URL Query Argument

Often I have the need to parse the URL query to retrieve the value of a specific named argument. There is no direct way of doing this is JavaScript, so this requires a custom function.


function findQueryArgument (strQuery, strArgumentName) {

    // Valid query?
    if (strQuery) {

        // Split the parameteres
        var arrParameters = strQuery.split("&");

        // Walk through the parameters
        for (var i = 0; i < arrParameters.length; i++) {
            // Get the key/value pair
            var arrPair = arrParameters[i].split("=");

            // Is this the event argument?
            if (arrPair[0] == strArgumentName) {
                // Has value?
                if (arrPair.length > 1) {
                    // Get the value
                    return arrPair[1];
                }

                // Not set
                break;
            }
        }
    }

    // Not found
    return '';
}

The strQuery argument is the full URL query i.e. a=val1&b=val2 etc. The full URL should not be included.
strArgumentName is the name of the argument you're looking for, i.e. a or b.

Please notice that the returned value is URL encoded. To extract the actual string value you need to use the built in JavaScript method unescape.

Here is an example:

// Query string
var strQuery = 'a=val1&b=val2';

// Get the value of the 'b' argument
var strEscaped = findQueryArgument(findQueryArgument, strQuery, 'b');

// Get the unescaped value
var strUnescaped = unescape(strEscaped);

// Display the value
alert(strUnescaped);

Saturday, October 8, 2011

MS SQL: Url Encode

There is no built-in function in Microsoft SQL Server to to support URL encoding. If you want to generate URLs with arguments on the fly in stored procedures, you would have to do the URL encoding yourself. URL encoding can be implemented in several ways. Either you can create your own custom userdefined function in MS SQL or create a custom library in .NET. The library can then be called from a stored procedure.

Here I will show how you can encode your URLs using a userdefined function:


CREATE FUNCTION [dbo].[audf_Common_UrlEncode](@strUrl varchar(max))
returns varchar(max)
AS
 begin
    -- Declare variables
    declare @intCount int,
            @strChar char(1),
            @i int,
            @strUrlReturn varchar(max)

    -- Initialize variables
    set @intCount       = Len(@strUrl);
    set @i              = 1;
    set @strUrlReturn   = '';  

    -- Loop through all characters
    while (@i <= @intCount)
    begin
        -- Get the character
        set @strChar = substring(@strUrl, @i, 1)

        -- Is ASCII character?
        if @strChar LIKE '[A-Za-z0-9()''*-._! ]'
         begin
            -- Just append character
            set @strUrlReturn = @strUrlReturn + @strChar
         end
        else
         begin
            -- Encode and append character
            set @strUrlReturn =
                   @strUrlReturn +
                   '%' +
                   SUBSTRING(sys.fn_varbintohexstr(CAST(@strChar as varbinary(max))),3,2)
         end
        -- Next character
        set @i = @i +1
     end

    -- Return the encoded URL
    return @strUrlReturn
 end