不常用又没什么卵用的js方法

By Heanny
2017-01-05
2666 read

修改url地址但是不刷新页面

const url = window.location;
const arr = url.pathname.split('/');
arr.pop();
arr.push('222');
const newUrl = url.origin + arr.join('/') + url.search;
history.pushState('', '', newUrl); // 不刷新页面
// url.href = newUrl; // 刷新页面
方法:window.history.pushState(state, title, url)

js修改地址栏参数

    function changeURLPar(destiny, par, par_value) {
            var pattern = par + '=([^&]*)';
            var replaceText = par + '=' + par_value;
            if (destiny.match(pattern)) {
                var tmp = '/' + par + '=[^&]*/';
                tmp = destiny.replace(eval(tmp), replaceText);
                return (tmp);
            }
            else {
                if (destiny.match('[\?]')) {
                    return destiny + '&' + replaceText;
                }
                else {
                    return destiny + '?' + replaceText;
                }
            }
            return destiny + '\n' + par + '\n' + par_value;
        }
f分享
javaweb(5)

Comments

暂无评论,还不快来坐沙发...

Leave a Reply