的位置.用url片段替换不一致的部分
location.replace inconsistencies with url fragments
我最近注意到location.replace
实际上并没有重新加载页面,当有一个url片段(#
)在当前url和目标url:
// Assume the current url is /#url-fragment
window.location.replace('/#my-new-url-fragment');
// Doesn't reload the page.
我正在寻找一个可靠的,跨浏览器的方式(支持IE8),以取代当前的url,而不添加到历史记录
这是我能做到的最好的答案,但它不是一个非常全面的答案。这更多的是一种试错,而不是真正的合理化行为。
我建立了一个所有可能场景的表,然后一个脚本来检查结果。在所有条件下都有效的逻辑是:
window.location.replace(url);
/* If the target url has a hash, and ignoring hashes the two urls match,
then the location.replace() call above *did not reload the page*. Do so now. */
var urlWithoutHash = ...; // /test#hash -> /test
var currentUrlWithoutHash = ...;
if (url.indexOf('#') >= 0 && urlWithoutHash == currentUrlWithoutHash)
window.location.reload();
相关文章:
- 当属性不一致时,如何根据属性对JS对象列表进行排序
- Android和JavaScript解析API之间不一致
- Javascript的行为与PHP包含文件不一致
- 对这种与document.domain和CORS相关的不一致行为的解释是什么
- 点击链接行为不一致
- 用javascript添加类,而不是替换
- Passport.js`isAuthenticated()`不一致的行为;当它应该是真的时候是假的
- 将数组存储到本地存储中,而不是替换
- 与显示不一致
- JavaScript 不会替换字符串的最后一个符号
- Ext 4.2.1 模型日期解析在浏览器之间不一致
- JavaScript .replace HTML 标记不会替换
- 通过 Dropbox API 上传时的文件内容不一致
- JavaScript - 对象属性不一致
- InnerHTML 追加项而不是替换项
- Javascript 混淆了 null、instanceof 和 typeof 的语法不一致
- localeCompare 显示使用前导变音字符对单词进行排序时不一致的行为
- SVG 对象部分上的 JQuery click() 不一致
- javascript.replacement()不会替换所有出现的内容
- 的位置.用url片段替换不一致的部分