常用脚本:重载、刷新 、当前、自身页面

应用场景:

需要定时刷新的页面;通过ajax更新后需要显示新数据等多种情况。

实现方法

有多少方法可以实现,如:

可代替方法 window.location.reload(true) 支持(ie ,chrome);

history.go(0)
window.location.reload()
window.location.reload(true) 
location=location
location.assign(location)
document.execCommand(''Refresh'')
window.navigate(location)
location.replace(location)
document.URL=location.href

考虑到兼容性,最后会多半会采用语句

window.location = window.location

在GET方法访问下,通常这个方法会在本地缓存中重新加载一次,于是有了这样的方法,给地址加一个随机数让浏览器去再次请求服务器地址。

代码如下

 function reloadLocation()
    {
        var t = new String(window.location);
        if (t.indexOf('r=') > 0) {
            t = t.replace(/r=[0123456789.]+/gi, 'r=' + Math.random());
        }
        else {
            if (t.indexOf('?') > 0) {
                t = t + '&r=' + Math.random();
            }
            else {
                t = t + '?r=' + Math.random();
            }
        }
        window.location = t;
   }

使用须知  

该方法会占用url r 参数,还请多多注意

如果大家有更好的方法,欢迎告知。

原文地址:https://www.cnblogs.com/zyug/p/4816503.html