javascript window.reload then window.location.href
javascript window.reload then window.location.href
我想编写一个代码,在其中重新加载窗口,然后更改位置,尽管我尝试了window.location.reload(); window.location.href="#id"
,但它不起作用,因为当页面重新加载时,脚本将不会运行
实际上,我想从页面中清除POST数据,然后将同一页面上的位置更改为id。
正如其他人所说,更新href
将触发重新加载,因此不需要手动调用reload
。如果你真的想这样做,那就需要进行条件检查,以防止你看到的无限次重新加载。。。比如基于url中的一些参数,比如:
if (window.location.hash === '') {
window.location.hash = '#id';
window.location.reload();
} else {
window.location.hash = '';
}
仅仅更新hash
不会导致重新加载,因此以下操作不起作用(它只会移动到位置)
window.location.href = '#answer-28520707').
然而,你可以这样做:
window.location.href = "?_reload" + Date.now() + "#answer-28520707"
查询参数将导致浏览器刷新页面,哈希将跳转到新位置。
如果你想测试这个,试着在控制台中粘贴上面的内容,或者在地址栏中键入:
javascript:void(window.location.href = "?_reload" + Date.now() + "#answer-28520707")
(它应该重新加载页面并滚动到此答案)
只需使用
window.location.href = "#id";
window.location.reload();
您不需要先重新加载,只需更改location.href值即可重新加载页面。
相关文章:
- 如何将所有作业自定义到window.location.href
- 如果window.location.href.indexOf('player=1')添加样式
- 可以'当window.location.href时,无法从javascript获取post值
- window.location.href/reload导致AngularJS[$rootScope:infdig]达到1
- 当用户在选择框中选择选项时,使用window.location.href重定向用户只会更改导航器中URL的最后一位
- window.location.href =“#find” 适用于移动设备,但不适用于 PC.在本地站点上正常(表达式
- Javascript-将变量传递给php页面,window.location.href不起作用
- 后退按钮在 window.location.replace(href);之后失败
- 在window.location.href中使用php
- 如何在Jquery Mobile和RequireJS中使用window.location.href时避免出现空白页
- 无法在Chrome中捕获Chrome window.location.href
- ie与chrome window.location.href和<基本>标签
- window.location.href窗口未在javascript中定义
- 脏表单 - 如何忽略在parent.window.location.href上弹出的脏表单
- 使用window.location.href进行完全重定向
- javascript window.reload then window.location.href
- window.location.href未设置查询参数
- 页面加载方式不同,window.location与href:
- 检查window.location.href.indexOf是否'不起作用
- window.location.replace或href时Web会话丢失