获取url参数

方法一: 

    function getParamValue( url, key ) {

        if ( typeof url !== "string" || typeof key !== "string" ) return "";

 

        var index = url.indexOf( key + "=");
        if ( index === -1 ) return "";

 

        url = url.substr( index + 1 + key.length );

 

        index = url.indexOf( "&" );
        return index === -1 ? url : url.substr( 0, index );
    }

方法二:

    function getParamValue( url, key ) {
        if ( typeof url !== "string" || typeof key !== "string" ) return "";
 

        var reg = new RegExp( "[?&]" + key + "=([^& ]*)&?" );

        var matches = reg.exec( url );
        if ( matches ) {
            return matches[1];
        } 
        return "";
    }

 

posted @ 2012-03-14 23:11  ihada  阅读(216)  评论(0编辑  收藏  举报