×

Web前端开发:用JS刷新页面

作者:andy0012019.11.22来源:Web前端之家浏览:7884评论:0
关键词:jsreload()

一起来学习下Web前端开发的小技巧:用JS刷新页面,介绍几种方法。

方法1:使用location.reload()

location.reload()方法重新加载当前web页面,此方法类似于你浏览器上的刷新页面按钮。如果把该方法的参数设置为 true参数,可强制页面从服务器加载并忽略浏览器缓存。

语法:

location.reload(true)

示例:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

</head>
  
<body>
    <h3 style="color: green">如何使用jQuery刷新页面?</h3>
    <p>这是一段测试文本!</p>
          输入文本:<input type="text" /><br /><br />
      
    <button type="button">重新加载页面</button>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"> </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("button").click(function () {
                location.reload(true);
                alert('Reloading Page');
            });
        });
    </script>
</body>
  
</html>

方法2:使用history.go(0)

history.go()方法根据传递给它的参数从浏览器历史记录中加载一个URL。如果传递的参数为“0”,则会重新加载当前页。

语法:

history.go(0);

示例:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

</head> 
  
<body> 
    <h3 style="color: green">如何使用jQuery刷新页面?</h3> 
    <p>这是一段测试文本!</p> 
          输入文本:<input type="text" /><br /><br />
      
    <button type="button">重新加载页面</button> 
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"> </script> 
    <script type="text/javascript"> 
        $(document).ready(function () { 
            $("button").click(function () { 
                history.go(0); 
            }); 
        }); 
    </script> 
</body> 
  
</html>

您的支持是我们创作的动力!
温馨提示:本文作者系 ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/js2038402384028304.html

网友评论文明上网理性发言 已有0人参与

发表评论: