Home > Web Front-end > JS Tutorial > body text

Javascript imitates PHP $_GET to get the parameters in the URL_javascript skills

WBOY
Release: 2016-05-16 16:48:40
Original
1285 people have browsed it

复制代码 代码如下:

/* 像PHP的 $_GET['arg'] 那样获得地址栏GET参数 */
function getArgs() {
    var args = {};
    var query = location.search.substring(1); // Get query string
    var pairs = query.split("&");
                   // Break at ampersand
     for(var i = 0; i < pairs.length; i ) {
            var pos = pairs[i].indexOf('=');
             // Look for "name=value"
            if (pos == -1) continue;  // If not found, skip
               var argname = pairs[i].substring(0,pos); // Extract the name
               var value = pairs[i].substring(pos 1); // Extract the value
               value = decodeURIComponent(value); // Decode it, if needed
               args[argname] = value;  // Store as a property
        }
    return args; // Return the object          
}

/* 使用方法 */
/* URL: http://www.baidu.com?user=funsion&age=26 */
alert( getArgs()['user'] );  // 输出 funsion
alert( getArgs()['age'] );  // 输出 26

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!