history. go( 0 )
- history对象有一个go()方法,go() 方法可加载历史列表中的某个具体的页面。
- 语法:history.go(number|URL)
- 说明:URL 参数使用的是要访问的 URL,或 URL 的子串。而 number 参数使用的是要访问的 URL 在 History 的 URL 列表中的相对位置。
- 加载历史列表中的前一个页面:window.history.go(-1),刷新当前页面 history.go(0);
<!DOCTYPE html>
<html lang="en">
<head>
<!-- 第一个meta指定编码,IDEA新建默认就有的,与Bootstarp无关-->
<meta charset="UTF-8">
<!-- BootCDN提供了很多如JQuery、Chart.js、ECarts.js等等,只需去复制来即可
BootCDN官网地址:http://www.bootcdn.cn/-->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
/** 刷新当前页面*/
$("#refresh").click(function () {
history.go(0);
});
});
</script>
</head>
<body>
<button id="refresh">刷新</button>
</body>
</html>
location. assgin( url )
- 定义和用法:assign() 方法可加载一个新的文档。
- 语法:location.assign(URL)
- 当前页面加载百度首页:location.assign("http://www.baidu.com");
<!DOCTYPE html>
<html lang="en">
<head>
<!-- 第一个meta指定编码,IDEA新建默认就有的,与Bootstarp无关-->
<meta charset="UTF-8">
<!-- BootCDN提供了很多如JQuery、Chart.js、ECarts.js等等,只需去复制来即可
BootCDN官网地址:http://www.bootcdn.cn/-->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#refresh").click(function () {
/** 刷新当前页面
* 将当前页面地址传入即可*/
location.assign(location.href);
});
});
</script>
</head>
<body>
<button id="refresh">刷新</button>
</body>
</html>
location. replace( newURL )
- 定义和用法:replace() 方法可用一个新文档取代当前文档。
- 语法:location.replace(newURL)
- 说明:replace() 方法不会在 History 对象中生成一个新的纪录。当使用该方法时,新的 URL 将覆盖 History 对象中的当前纪录。
- 用百度首页替换当前文档:location.replace("http://www.baidu.com");
<!DOCTYPE html>
<html lang="en">
<head>
<!-- 第一个meta指定编码,IDEA新建默认就有的,与Bootstarp无关-->
<meta charset="UTF-8">
<!-- BootCDN提供了很多如JQuery、Chart.js、ECarts.js等等,只需去复制来即可
BootCDN官网地址:http://www.bootcdn.cn/-->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#refresh").click(function () {
/** 刷新当前页面
* 将当前页面地址作为新文档地址传入即可*/
location.replace(location.href);
});
});
</script>
</head>
<body>
<button id="refresh">刷新</button>
</body>
</html>
自动间隔刷新
- 使用:<meta http-equiv="refresh" content="x"> 元信息设置,即每间隔 x 秒之后刷新当前页面
-
<!-- 3秒之后跳转到百度首页--> <meta http-equiv="refresh" content="3;url=http://www.baidu.com">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- 间隔1秒刷新当前页面-->
<meta http-equiv="refresh" content="1">
<!-- BootCDN提供了很多如JQuery、Chart.js、ECarts.js等等,BootCDN官网地址:http://www.bootcdn.cn/-->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#refresh").click(function () {
});
});
</script>
</head>
<body>
<button id="refresh">刷新</button>
</body>
</html>
location. href = URL
- 定义和用法:href 属性是一个可读可写的字符串,可设置或返回当前显示的文档的完整 URL。
- 因此可以通过为该属性设置新的 URL,使浏览器读取并显示新的 URL 的内容。
- 语法:location.href=URL
- 当前页面上打开百度首页:location.href = "http://www.baidu.com";
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- BootCDN提供了很多如JQuery、Chart.js、ECarts.js等等,BootCDN官网地址:http://www.bootcdn.cn/-->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#refresh").click(function () {
/**
* 将当前页面地址作为新地址即可刷新当前页面
* @type {string}
*/
location.href = location.href;
});
});
</script>
</head>
<body>
<button id="refresh">刷新</button>
</body>
</html>
location.reload( force )
- 定义和用法:reload() 方法用于重新加载当前文档。这是实际开发中刷新页面常用的方式。
- 语法:location.reload(force)
- 说明:
- 如果该方法没有规定参数,或者参数是 false,它就会用 HTTP 头 If-Modified-Since 来检测服务器上的文档是否已改变。如果文档已改变,reload() 会再次下载该文档。如果文档未改变,则该方法将从缓存中装载文档。这与用户单击浏览器的刷新按钮的效果是完全一样的。
- 如果把该方法的参数设置为 true,那么无论文档的最后修改日期是什么,它都会绕过缓存,从服务器上重新下载该文档。这与用户在单击浏览器的刷新按钮时按住 Shift 健的效果是完全一样。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- BootCDN提供了很多如JQuery、Chart.js、ECarts.js等等,BootCDN官网地址:http://www.bootcdn.cn/-->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#refresh").click(function () {
/** 将当前页面地址作为新地址即可刷新当前页面*
* 如果要强制收刷新,则为: location.reload(true);
*/
location.reload();
});
});
</script>
</head>
<body>
<button id="refresh">刷新</button>
</body>
</html>
window.open
- 定义和用法:open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口。
- 语法:window.open(URL,name,features,replace)
- 在新窗口中打开百度首页:window.open("http://www.baidu.com");
参数 | 描述 |
---|---|
URL | 一个可选的字符串,声明了要在新窗口中显示的文档的 URL。如果省略了这个参数,或者它的值是空字符串,那么新窗口就不会显示任何文档。 |
name | 一个可选的字符串,该字符串是一个由逗号分隔的特征列表,其中包括数字、字母和下划线,该字符声明了新窗口的名称。这个名称可以用作标记 <a> 和 <form> 的属性 target 的值。如果该参数指定了一个已经存在的窗口,那么 open() 方法就不再创建一个新窗口,而只是返回对指定窗口的引用。在这种情况下,features 将被忽略。 |
features | 一个可选的字符串,声明了新窗口要显示的标准浏览器的特征。如果省略该参数,新窗口将具有所有标准特征。在窗口特征这个表格中,我们对该字符串的格式进行了详细的说明。 |
replace | 一个可选的布尔值。规定了装载到窗口的 URL 是在窗口的浏览历史中创建一个新条目,还是替换浏览历史中的当前条目。支持下面的值:
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- BootCDN提供了很多如JQuery、Chart.js、ECarts.js等等,BootCDN官网地址:http://www.bootcdn.cn/-->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#refresh").click(function () {
/**在新窗口中打开本页面*/
window.open(location.href);
});
});
</script>
</head>
<body>
<button id="refresh">刷新</button>
</body>
</html>