如何从html页面中的url获取参数并将其显示在textarea中?

时间:2013-07-12 07:04:15

标签: javascript html ajax

我在我的html页面中使用以下javascript函数,我希望在textarea中没有任何连接运算符的情况下正确显示参数....

<html>
    <head>
        <script type="text/javascript">
            function getParameter(paramName) {
                var searchString = window.location.search.substring(1), i, val, params = searchString.split("&");

                for ( i = 0; i < params.length; i++) {
                    val = params[i].split("=");
                    if (val[0] == paramName) {
                        return val[1];
                    }
                }
                return null;
            }

            function getUrlData() {
                var comment = getParameter("comment");
                document.getElementsByName("my-textarea")[0].value = comment;
            }

        </script>
    </head>
    <body onload="getUrlData()">
        <h1>welcome</h1>
        <textarea name="my-textarea" readonly rows="5" cols="30"> </textarea>       


    </body>
</html>

1 个答案:

答案 0 :(得分:0)

试试这个

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}