locked
Explanation of function RRS feed

  • Question

  • User-1535678595 posted

    I found this function and in basic i know what it does.

    But i was wondering if there was a friendly soul in here, that is better at jquery than me and can explain this.

    function getParameterByName(name, url) {
            if (!url) url = window.location.href;
            name = name.replace(/[\[\]]/g, "\\$&");
            var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                results = regex.exec(url);
            if (!results) return null;
            if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/\+/g, " "));
        }

    Would help me alot to understand it better.

    Wednesday, June 8, 2016 8:57 AM

Answers

  • User281315223 posted

    To be clear, there isn't really any jQuery code in here at all, as it's pure vanilla Javascript and is a function to read a querystring value from an existing URL :

    function getParameterByName(name, url) {
            // If no URL was passed into the function, then use the current one
            if (!url) url = window.location.href;
            // Explicitly replace any square braces in the name of the attribute you are looking for
            // i.e. foo[] => foo\[\] so that it can be used in a regular expression later
            name = name.replace(/[\[\]]/g, "\\$&");
            // Build an expression that will use your value that is proceeded by either a '?' or '&' character and a group
            // to match its value
            var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            // Attempt to find a match in the URL
            results = regex.exec(url);
            // If none were found, then return null 
            if (!results) return null;
            // If a second group was empty, return an empty string (i.e. a parameter with no value like ?foo)
            if (!results[2]) return '';
            // Otherwise you have a result, so decode it and replace any + characters with spaces
            return decodeURIComponent(results[2].replace(/\+/g, " "));
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 8, 2016 3:00 PM

All replies

  • User281315223 posted

    To be clear, there isn't really any jQuery code in here at all, as it's pure vanilla Javascript and is a function to read a querystring value from an existing URL :

    function getParameterByName(name, url) {
            // If no URL was passed into the function, then use the current one
            if (!url) url = window.location.href;
            // Explicitly replace any square braces in the name of the attribute you are looking for
            // i.e. foo[] => foo\[\] so that it can be used in a regular expression later
            name = name.replace(/[\[\]]/g, "\\$&");
            // Build an expression that will use your value that is proceeded by either a '?' or '&' character and a group
            // to match its value
            var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            // Attempt to find a match in the URL
            results = regex.exec(url);
            // If none were found, then return null 
            if (!results) return null;
            // If a second group was empty, return an empty string (i.e. a parameter with no value like ?foo)
            if (!results[2]) return '';
            // Otherwise you have a result, so decode it and replace any + characters with spaces
            return decodeURIComponent(results[2].replace(/\+/g, " "));
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 8, 2016 3:00 PM
  • User61956409 posted

    Hi dentjavy,

    Firstly, Rion has gave good comments to the code, you could refer to the comments to understand the code.

    Secondly, for details and usages of some main JavaScript properties and methods used in the code, you could check these.

    if (!url) url = window.location.href;

    window.location.href returns the href (URL) of the current page

    name = name.replace(/[\[\]]/g, "\\$&");

    The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.

    results = regex.exec(url);

    The exec() method searches a string for the specified character, and returns the matched text if it finds a match, otherwise it returns null.

    Best Regards,

    Fei Han

    Thursday, June 9, 2016 2:32 AM