0

I trying to learn HTML by studying code from a downloaded web site. In the HTML code is the following snippet of Javascript. I am so new to Javascript and HTML and have no clue what this snippet is for. Any ideas?

function gID(name)
    {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.search );
        if( results == null )
            return "";
        else
            return results[1];
    }
3
Contributors
3
Replies
5
Views
4 Years
Discussion Span
Last Post by aiki985
0

Hi,
That function looks for the parameter "name" in the curent page address.
The code inside function filters the "name" and returns the result if it is find in the page URL, or empty data.

0
1.function gID(name) {
2.    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
3.    var regexS = "[\\?&]"+name+"=([^&#]*)";
4.    var regex = new RegExp( regexS );
5.    var results = regex.exec( window.location.search );
6.    if( results == null )
7.        return "";
8.    else
9.        return results[1];
}

Line 1, declare function prototype (name & arguments)
Line 2, escape characters '[' and ']' by adding a backslash in front of them (need this for regular expression)
Line 3, create a regular expression string that start with either a question mark '?' or ampersand '&', follows by the incoming argument, and ends with an equal sign and any of at composition characters '^', '&', and '#' or none.
Line 4, create a regular expression object
Line 5, attempt to match the regular expression with the current window's argument call (for GET method, i.e. http://www.domainname.com/page.html?v=1,p=2 which results in "?v=1,p=2" for window.location.search)
Line 6, check if it could match
Line 7, return an empty string if not found the match
Line 8, else
Line 9, return the matched if found. The match is after will be any pattern inside the parenthesis in the regular expression (any char sequence right after & or # including '&' or '#').

I'm not sure if the regular expression is what you need though...

Edited by Taywin

0

MarPlo, Taywin,

Thank you both! That helps me a lot.

aiki985

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.