[javascript][轉貼]使用javascript來get參數

摘要:[javascript][轉貼]使用javascript來get參數

最近剛好有這類需求!!因為需要把之前瀏覽的網頁在做完驗證後導回去所以使用了這樣的方法

感謝網路上大大的分享

 var queryString = window.top.location.search.substring(1);
        function getParameter(queryString, parameterName) {
            // Add "=" to the parameter name (i.e. parameterName=value)
            var parameterName = parameterName + "=";

            if (queryString.length > 0) {
                // Find the beginning of the string
                begin = queryString.indexOf(parameterName);
                // If the parameter name is not found, skip it, otherwise return the value
                if (begin != -1) {
                    // Add the length (integer) to the beginning
                    begin += parameterName.length;
                    // Multiple parameters are separated by the "&" sign
                    end = queryString.indexOf("&", begin);
                    if (end == -1) {
                        end = queryString.length
                    }
                    // Return the string
                    return unescape(queryString.substring(begin, end));
                }
                // Return "null" if no parameter has been found
                return "null";
            }

 

 

 if (getParameter(queryString, 'ggg') == 'ccc') {
        alert("接收到的是ccc");
    } else {
        alert("接收到的不是ccc")
    }