讀取 url JQuery 中的 GET 變量 (read the GET variables in url JQuery)


問題描述

讀取 url JQuery 中的 GET 變量 (read the GET variables in url JQuery)

Sorry for another "simple" question, but is there an easy way to read the GET variables from a URL.  example. I have a url http://www.domain.com/page.php?var1=1  In my case I will only have 1 variable i.e. var1 or var2 (the variable can change but there will only every be one per url).  All the tuts I have seen relate to arrays rather than "singletons" OK I know an array solution may be better but this is just a simple single get variable.  Any suggestions? Thanks in advance


參考解法

方法 1:

var split = location.search.replace('?', '').split('=')

split[0] is your var name, and split[1] is your var value. You actually don't really need jQuery for that piece of code ;)

As for twiz's comment, splitting multiple variables can be done like that:

var split = location.search.replace('?', '').split('&').map(function(val){
  return val.split('=');
});

You can access variable name by split[index][0] and value by split[index][1].

Of course you can use the second snippet instead of the first one for one variable too.

方法 2:

I use this in my default javascript file.

var get = [];
location.search.replace('?', '').split('&').forEach(function (val) {
    split = val.split("=", 2);
    get[split[0]] = split[1];
});

Now you can use them by name:

get["var1"]

方法 3:

You can use this function that returns the value of the specified var name from url if the var exists. Otherwise you will get an empty string.

function getUrlValue(varName) {
    var split = $(location).attr('href').split('?');
    var value = '';
    if (split.length == 2) {
        split = split[1].split('&');
        for (var i = 0; i < split.length; i+=1) {
            var keyValue = split[i].split('=');
            if (keyValue.length == 2 && keyValue[0] == varName) {
                value = keyValue[1];
                break;
            }
        }
    }
    return value;
}

方法 4:

With ES2019 to create an object:

let GET = Object.fromEntries( location.search                                       .replace( '?', '' )                                       .split( '&' )                                       .map( i => i.split( '=' ) ) );

The Object.fromEntries method creates an object from an array.

For the URL http://www.example.com/page?var1=1&var2=2 the above code returns the object

{
  var1: '1',
  var2: '2'
}

(by Russell ParrottusobanTiele Declercqungalcrysjla)

參考文件

  1. read the GET variables in url JQuery (CC BY-SA 3.0/4.0)

#Variables #jquery #javascript #get






相關問題

使用 htaccess 從基本 URL 中刪除變量 (Remove variable from base URL with htaccess)

如何將解碼的數據包數據和標頭輸出組合到單個變量/字典中以進行字符串搜索 (How to combine decoded packet data and header output into a single variable/dictionary for string searches)

@Ruby on Rails 中的變量 (@ variables in Ruby on Rails)

xslt 將變量發送到另一個模板 (xslt send variable to another template)

變量被評估為任何東西 (Variable Being Evaluated as Anything)

獲取python函數的變量 (Get a variable of python function)

讀取 url JQuery 中的 GET 變量 (read the GET variables in url JQuery)

使用變量名作為數組索引 (Using variable name as array index)

Javascript PHP var數組將雙引號放入 (Javascript PHP var array puts double quotes in)

兩個不同定義之間的變量 (Variable between two different definitions)

Tkinter 組合框 Python (Tkinter Combobox Python)

有沒有辦法在 Python 中使用變量中的字符串調用方法? (Is there a way to call a method in Python using a string inside a variable?)







留言討論