关于javascript跳转与返回和刷新页面——深入理解

it2025-11-27  10

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

window.open(‘index.html’) 表示新增一个窗口打开 index.html 这个页面,并不刷新 location.href(‘index.html’) 表示在当前窗口重定向到新页面,打开并刷新 index.html 这个页面

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>

JS举例:

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

到这里就结束了喜欢的小伙伴可以收藏一下,有写不好可以评论一下提提意见~

最新回复(0)