アドレスバーのURLの後ろにJSのパラメータを取得するには?

この記事は、もはや更新され、古くなったコンテンツのインスタンスが存在しない場合がある、リアルタイムの更新は私の新しいブログへの移行を喜ば:どのようにJSアドレスバーのURLの後ろにパラメータを取得するには?;

ここではアドレスバーのURLパラメータの後ろに取得するための2つの方法があります。

モード1

バイオグラフィーリファレンス:

window.location.href = "/html/bsp/user/userEdit.html?name=四个空格&age=2";

取得パラメータ:

function getParams() {
    var params = {};
    if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
        var paramArray = unescape(this.location.search).substring(1, this.location.search.length).split("&");
        if (paramArray.length > 0) {
            paramArray.forEach(function (currentValue) {
                params[currentValue.split("=")[0]] = currentValue.split("=")[1];
            });
        }
    }
    return params;
}

var name = getParams().name;

モード2

バイオグラフィーリファレンス:

var params = {};
params['name'] = '四个空格';
params['age'] = '2';
window.location.href = "/html/bsp/user/userEdit.html?" + new URLSearchParams(params);

取得パラメータ:

function urlParams(){
    var searchParams;
    if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
        searchParams = new URLSearchParams(this.location.search.substring(1, this.location.search.length));
    }
    return searchParams;
}

var name = urlParams().get('name');

参考記事:

  1. URLSearchParams ;
  2. URLパラメータをJSONに変換する任意のネイティブ機能はありますか?

おすすめ

転載: www.cnblogs.com/cobcmw/p/11997693.html