关于javascript跳转与返回和刷新页面的问题

猿友 2021-03-01 17:31:01 浏览数 (7479)
反馈

javascript 中​window.open()​与​window.location.href​的区别

1、window.open('index.html') 表示新增一个窗口打开 index.html 这个页面,并不刷新

location.href('index.html') 表示在当前窗口重定向到新页面,打开并刷新 index.html 这个页面

2、window.location 是 window 对象的属性,用来替换当前页,也就是重新定位当前页

而 window.open 是 window 对象的方法,是用来打开一个新窗口的函数

// 打开新页面
// 注意:有些浏览器的安全设置会将window.open()屏蔽,例如避免弹出广告窗
window.open('./index.html');

// 在原窗口打开新页面
window.location.href="./index.html";

window.open()详解

window.open ('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no')

参数解释: 三个参数

  • window.open 弹出新窗口的命令;
  • 'page.html' 弹出窗口的文件名;
  • 'newPage' 弹出窗口的名字(不是文件名),非必须,可用空’'代替;
  • height=100 窗口高度;
  • width=400 窗口宽度;
  • top=0 窗口距离屏幕上方的象素值;
  • left=0 窗口距离屏幕左侧的象素值;
  • toolbar=no 是否显示工具栏,yes 为显示;
  • menubar=no 是否显示菜单栏,yes 为显示;
  • scrollbars=no 是否显示滚动栏,yes 为显示;
  • resizable=no 是否允许改变窗口大小,yes 为允许;
  • location=no 是否显示地址栏,yes 为允许;
  • status=no 是否显示状态栏内的信息(通常是文件已经打开),yes为允许;

常用的 js 返回与刷新页面

在此用 a 标签举例

<a href="javascript: history.back(-1)">返回上一页</a> 
<a href="javascript:history.go(-1)">返回上一页</a> 
<a href="javascript:history.go(-2)">返回前两页</a> 
<a href="javascript:location.reload()">刷新当前页面</a> 
<a href="javascript:" onclick="self.location=document.referrer;">返回上一页并刷新</a> 

JavaScript

// 刷新当前页面
window.location.Reload();
self.location.reload();
window.location.href = window.location.href; 


0 人点赞