Read the query string using JavaScript to read the query string [querystring]

zhaozj2021-02-16  180

// Read the query string in the way in the key value

//, for example, [http://localhost/test.htm? First = 1 & second = 2]

Function getQueryString (Key)

{

VAR value = ""

/ / Get the URL of the current document, analyze it back to prepare

VAR SURL = WINDOW.Document.URL;

Whether or not to contain query strings in // URL

IF (SURL.INDEXOF ("?")> 0)

{

/ / Decompose the URL, the second element is a complete query string

// The value of ArrayParams [1] is [first = 1 & second = 2]

Var ArrayParams = SURL.SPLIT ("?");

// Decompose query string

// arrayurlparams [0] is [first = 1]

// arrayurlparams [2] is [Second = 2]

Var arrayurlparams = arrayparams [1] .split ("&");

// Traverage the key value of the decomposition

For (var i = 0; i

{

// Decompose a key value

VAR spram = arrayurlparams [i] .split ("=");

IF (Spram [0] == Key) && (Spram [1]! = ""))

{

/ / Find the matching key and the value is not empty

Value = spram [1];

Break;

}

}

}

Return Value;

} Use this method to read querystring, which may not agree with the pursuit of efficiency, but I consider that queryString will not be too much too much in a URL. And use this layer of peeling, step-by-step approach, more conformity Man's way of thinking.

转载请注明原文地址:https://www.9cbs.com/read-7738.html

New Post(0)