Example of parameter in JS replacing URL

<script type="text/javascript">
                var url = window.location.href;
                url = changeURLArg(url, "key", "Search value "),
                window.location.href = url;

                /*
                * url target url* Arg the parameter name that needs to be replaced.* the value of the parameter after arg_val is replaced.* return urURL after L parameter substitution*/
                function changeURLArg(url, arg, arg_val) {
                    var pattern = arg + '=([^&]*)';
                    var replaceText = arg + '=' + arg_val;
                    if (url.match(pattern)) {
                        var tmp = '/(' + arg + '=)([^&]*)/gi';
                        tmp = url.replace(eval(tmp), replaceText);
                        return tmp;
                    } else {
                        if (url.match('[\?]')) {
                            return url + '&' + replaceText;
                        } else {
                            return url + '?' + replaceText;
                        }
                    }
                    return url + '\n' + arg + '\n' + arg_val;
                }
            </script>

 

Leave a Reply

Your email address will not be published. Required fields are marked *