获取查询字符串值并将其显示在我的 html 页面中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15653554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:53:40  来源:igfitidea点击:

Get the query string value and display it in my html page

javascripthtmlquery-string

提问by Venil Aravazhi

After redirecting to home.html page, i can see the querystring values which i had given in the previous page.

重定向到 home.html 页面后,我可以看到我在上一页中给出的查询字符串值。

Home.html?FirstName=dd&LastName=ee&smtButton=Submit

And am getting the result as:

我得到的结果是:

firstname = undefined
lastname = undefined
age = undefined

Could anyone help me to solve this?

谁能帮我解决这个问题?

JS:

JS:

function getParams() {
    var idx = document.URL.indexOf('?');
    var params = new Array();
    if (idx != -1) {
        var pairs = document.URL.substring(idx + 1, document.URL.length).split('&');
        for (var i = 0; i < pairs.length; i++) {
            nameVal = pairs[i].split('=');
            params[nameVal[0]] = nameVal[1];
        }
    }
    return params;
}

params = getParams();
firstname = unescape(params["firstname"]);
lastname = unescape(params["lastname"]);
age = unescape(params["age"]);
document.write("firstname = " + firstname + "<br>");
document.write("lastname = " + lastname + "<br>");
document.write("age = " + age + "<br>");

采纳答案by rab

Inside your function function getParams()you are declared variable var params = new Array();, I think this makes confusion for you

在您的函数中,function getParams()您被声明为变量var params = new Array();,我认为这会让您感到困惑

if a match is found , you assigned url param like params[nameVal[0]] = nameVal[1];, this actually not adding value into array object. so params.lengthis 0.but it will works as array is instance of object .. ie params instanceof Objectis true

如果找到匹配项,则您分配了 url 参数,例如params[nameVal[0]] = nameVal[1];,这实际上并未将值添加到数组对象中。所以params.length0 。但它会工作,因为数组是对象的实例 .. 即params instanceof Objecttrue

so change into basic object .. to avoid confusion

所以改为基本对象..以避免混淆

function getParams() {
    var idx = document.URL.indexOf('?');
    var params = {}; // simple js object
    .. here goes other code
}

and object key is case sensitive, so FirstNamewill work ..

和对象键区分大小写,所以FirstName会工作..

firstname = unescape(params["FirstName"]);

to print all values try this

打印所有值试试这个

params = getParams();

for( var i in params ){
    console.log( i , params[i] );
}

it will print

它会打印

FirstName dd    
LastName ee    
smtButton Submit

and I have modified your getParams code

我修改了你的 getParams 代码

function getParams() {

    var params = {},
        pairs = document.URL.split('?')
               .pop()
               .split('&');

    for (var i = 0, p; i < pairs.length; i++) {
           p = pairs[i].split('=');
           params[ p[0] ] =  p[1];
    }     

    return params;
}

回答by KooiInc

You can convert a querystring from the location.search-string to a js-object using:

您可以使用以下方法将查询字符串从location.search-string 转换为 js-object:

String.prototype.q2obj = function(){
    var qArr = this.split('&')
        ,qObj = {}
        ,i =-1;
    while(++i<qArr.length) {
            qfrag = qArr[i].split('=');
            qObj[qfrag[0]] = qfrag[1];
   }
   return qObj;
};
//usage
var queryObj = location.search.substr(1).q2obj();