点击按钮时刷新页面

29

当按钮被点击时,我希望能够刷新当前页面。

使用JavaScript,我有以下代码:

<button type="button" onClick="refreshPage()">Close</button>

<script>
    function refreshPage() {
        // What do I put here?
    } 
</script>

如何编写代码以刷新当前页面?

8个回答

52
<button type="button" onClick="refreshPage()">Close</button>

<script>
function refreshPage(){
    window.location.reload();
} 
</script>
或者
<button type="button" onClick="window.location.reload();">Close</button>

window.location.reload() 在 IE 10 上无效,你能帮忙吗? - MustafaP

14

使用按钮或其他触发器重新加载当前页面有多种方法。以下示例使用按钮单击重新加载页面,但您可以使用文本超链接或任何您喜欢的触发器。

<input type="button" value="Reload Page" onClick="window.location.reload()">

<input type="button" value="Reload Page" onClick="history.go(0)">

<input type="button" value="Reload Page" onClick="window.location.href=window.location.href">

3
适用于所有浏览器。
<button type="button" onClick="Refresh()">Close</button>

<script>
    function Refresh() {
        window.parent.location = window.parent.location.href;
    }
</script>

0
<button onclick=location=URL>Refresh</button>

小技巧。


@RufusVS 不需要。 - Thielicious

0

开发刷新应用程序!
复制并运行!

<!DOCTYPE html>
<html>
<head>
    <title>Refresher App</title>
    <style>
        body {
            text-align: center;
            background-color: brown;
        }
        #container {
            color: white;
        }
    </style>
</head>
<body>
    <div id="container">
        <h1 id="counter">0</h1>
    </div>
</body>
<script>
    var counter = document.getElementById("counter").textContent;
    //For Counter
    setInterval(function () {
        counter++;
        document.getElementById("counter").textContent = counter;
    }, 1000);
    //For Reload
    setInterval(function () {
        window.location.reload();
    }, 10000)
</script>
</html>

1
你需要所有的CSS来回答这个问题吗?而且这里的代码是在超时时刷新,而OP正在询问按钮刷新。能否编辑/简化代码,只包含所需内容并准确回答OP的问题?同时发布一个jsfiddle也会很有帮助。 - Anton Krug

0

您可以轻松地添加Location reload()方法。

// What do I put here?
   location.reload();

0

这个问题实际上与JSP无关,它与HTTP有关。你可以这样做:

window.location = window.location;


1
你不是在混淆HTTP和JavaScript吗? - BalusC

0
我建议使用以下代码:<a href='page1.jsp'>刷新</a>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接