Home > Web Front-end > JS Tutorial > javascript 分享一个获取url get参数的方法

javascript 分享一个获取url get参数的方法

WBOY
Release: 2016-06-01 09:54:28
Original
1331 people have browsed it

本函数使用到了javascript中的indexOf方法和split方法。具体代码如下:

<code class="language-javascript">function getPara(para) {
    if (location.href.indexOf("?") == -1) {
        // 没有参数,则Do nothing.  
        return null;
    } else {
        // 取得GET请求?号后面的字符串  
        var urlQuery = location.href.split("?");
        if (urlQuery[1].indexOf("&") == -1) { //只有一个参数  
            if (urlQuery[1].indexOf("=") == -1) {
                //没有等号,没有参数,则Do nothing  
                return null;
            } else {
                var keyValue = urlQuery[1].split("=");
                var key = keyValue[0];
                var value = keyValue[1];
                if (key == para) {
                    return value;
                }
            }
        } else {
            // 解析参数  
            var urlTerms = urlQuery[1].split("&");
            for (var i = 0; i </code>
Copy after login

直接调用getPara函数即可获取某个url的参数。

本文章的js/html/php/css代码均可以复制到这个页面进行在线调试,你不妨试一下。

http://www.manongjc.com/runcode

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