We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Function ?

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
2 Weeks
Discussion Span
9 Months Ago
Last Updated
5
Views
aiki985
Newbie Poster
4 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

MarPlo
Junior Poster in Training
55 posts since Apr 2012
Reputation Points: 1
Solved Threads: 15
Skill Endorsements: 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...

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 376
Skill Endorsements: 17

MarPlo, Taywin,

Thank you both! That helps me a lot.

aiki985

aiki985
Newbie Poster
4 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0673 seconds using 2.69MB