How to read get request using Javascript?

后端 未结 5 773
孤城傲影
孤城傲影 2021-01-06 00:49

So I have html page called A.html it was called like this from B.html : A.html?varString=\"bla-bla-bla\" Is it correct for sending ar

5条回答
  •  星月不相逢
    2021-01-06 01:21

    To parse the query string through JS, you can view use something like

    function getQueryValue(param) {
            var queryStringArray = querystring && querystring.substring(1).split("&");
            for (var i=0, length = queryStringArray.length; i < length; i++) {
                 var token = queryStringArray[i],
                     firstPart = token && token.substring(0, token.indexOf("="));
                 if (firstPart === param ) {
                     return token.substring(token.indexOf("=") + 1, token.length);
                }
            }
     }
    

    E.g. Given a URL "http://domain.com.au?aaa=bbb , you can call this fn as getQeuryValue("aaa") and you'll get "bbb"

    I uploaded this code on Gist (bit modified to be compliant with a module pattern).

提交回复
热议问题