最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript get url segment and parameter - Stack Overflow

programmeradmin7浏览0评论

I've read some question but I still can't figure out how to do it
I have a url example/event/14aD9Uxp?p=10

Here I want to get the 14aD9Uxp and the value of p
I've tried using split('/'+'?p=') but it doesn't work
I want to use regex but I dont really understand how to use it

I've read some question but I still can't figure out how to do it
I have a url example./event/14aD9Uxp?p=10

Here I want to get the 14aD9Uxp and the value of p
I've tried using split('/'+'?p=') but it doesn't work
I want to use regex but I dont really understand how to use it

Share Improve this question asked Feb 28, 2016 at 7:27 SpadaboyzSpadaboyz 3692 gold badges5 silver badges18 bronze badges 3
  • 1 split by /, get last element. Again split by ?, first one is the 14a...., again split second by = and second one from last split array is 10. – Tushar Commented Feb 28, 2016 at 7:32
  • Regex way \/([^\/?]*?)\?p=(\d+)$ – Tushar Commented Feb 28, 2016 at 7:37
  • You might want to take a look at the location object. – Leo Commented Feb 28, 2016 at 7:40
Add a ment  | 

5 Answers 5

Reset to default 15
var URL='example./event/14aD9Uxp?p=10';

var arr=URL.split('/');//arr[0]='example.'
                       //arr[1]='event'
                       //arr[2]='14aD9Uxp?p=10'

var parameter=arr[arr.length-1].split('?');//parameter[0]='14aD9Uxp'
                                           //parameter[1]='p=10'

var p_value=parameter[1].split('=')[1];//p_value='10';

I've created a generalized function (restricted in some ways) that will return the GET value given the parameter. However this function will only work correctly provided that you do not Rewrite the URL or modify the URL GET SYNTAX.

//Suppose this is your URL "example./event/14aD9Uxp?p=10";
            function GET(variable) {
                var str = window.location.href;


                str = str.split("/");
                // str = [example., event, 14aD9Uxp?p=10]


                //Get last item from array because this is usually where the GET parameter is located, then split with "?"
                str = str[str.length - 1].split("?");
                // str[str.length - 1] = "14aD9Uxp?p=10"
                // str[str.length - 1].split("?") = [14aD9Uxp, p=10]

                // If there is more than 1 GET parameter, they usually connected with Ampersand symbol (&). Assuming there is more, we need to split this into another array

                str = str[1].split("&");
                // Suppose this is your URL: example./event/14aD9Uxp?p=10&q=112&r=119
                // str = [p=10, q=112, r=119]
                // If there is only 1 GET parameter, this split() function will not "split" anything

                //Remember, there might only be 1 GET Parameter, so lets check length of the array to be sure.
                if (str.length > 1) {
                    
                    // This is the case where there is more than 1 parameter, so we loop over the array and filter out the variable requested
                    for (var i = 0; i < str.length; i++) {

                        // For each "p=10" etc. split the equal sign 
                        var param_full_str = str[i].split("=");
                        // param_full_str = [p, 10]

                        //Check if the first item in the array (your GET parameter) is equal to the parameter requested
                        if (param_full_str[0] == variable) {
                            // If it is equal, return the second item in the array, your GET parameter VALUE
                            return param_full_str[1];
                        }
                    }
                } else {
                    // This is the case where there is ONLY 1 GET parameter. First convert it to a String Type because Javascript decided that str was no longer a String
                    // Now split it with the equal sign.
                    str = str.toString().split("=");
                    return str[1];
                }
            }
            document.write(GET("p"));
const url = new URL('http://example./event/14aD9Uxp?p=10');

const [,, eventId ] = url.pathname.split('/');
const p = url.searchParams.get('p');

Browser support:
https://caniuse./#feat=url
https://caniuse./#feat=urlsearchparams

function $_GET(param) {
    var vars = {};
    window.location.href.replace( 
        /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
        function( m, key, value ) { // callback
            vars[key] = value !== undefined ? value : '';
        }
    );

    if ( param ) {
        return vars[param] ? vars[param] : null;    
    }
    return vars;
}

I have collected this from here: http://www.creativejuiz.fr/blog/javascript/recuperer-parametres-get-url-javascript It works great.

To use it just grab your parameter like:

var id = $_GET('id');

Simple no-regex way

var s = "example./event/14aD9Uxp?p=10";
var splitByForwardSlash = s.split('/');

// To get 14aD9Uxp
splitByForwardSlash[splitByForwardSlash.length-1]

// To get p=10
splitByForwardSlash[splitByForwardSlash.length-1].split('?')[1]

I think you know how to go from here :-)

发布评论

评论列表(0)

  1. 暂无评论