使用#hash从地址栏刷新页面 [英] Page refresh from address bar with #hash

查看:522
本文介绍了使用#hash从地址栏刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

url: http://mydomain.com/test.html#somehash

测试脚本:

$(document).ready(function () {
    console.log("page initiated");
    if (window.location.hash) {
        console.log("hash changed (if-statement)");
    }

    $(window).on("hashchange", function () {
        console.log("hash changed (on statement)");
    });

});

也可以在小提琴上找到该脚本,但是该环境不适合显示行为.

The script can also be found on fiddle, but the environment is not suitable to display the behavior.

我遇到的问题是,当我单击浏览器地址栏并按Enter而不进行任何更改时.

The problem I get is when I click on the browser addressbar and hit enter without any changes.

  1. 当浏览器URL中包含#号时,不会重新启动该页面. document.ready不会被解雇,我也不会收到任何控制台消息.

  1. When the browser url has a hashtag, the page is not re-initiated. document.ready does not get fired and I do not get any console messages.

当浏览器URL中没有井号标签时,页面确实会重新启动,并且会触发document.ready.

When the browser url does not have a hashtag, the page does get re-initiated and document.ready is fired.

有人对此行为有一个解释,是否可以抓住它,以便在情况1中重新加载该页面? 因为我似乎找不到任何文档,所以在某处有文档吗?

Does anybody have an explanation for this behavior and can it be caught so that in situation 1 the page does get reloaded? Is there documentation somewhere, because I can't seem to find any?

推荐答案

这是您期望的行为.如果您的URL包含哈希字符串,则不应启动整页刷新.

This is desired behaviour. If your URL contains a hash string, it's not supposed to initiate a full page refresh.

在URL上添加哈希表示您要在页面内导航,而不是导航到新页面.单击刷新"按钮表示您要重新请求文档.在网址栏上按Enter键,然后单击刷新"是完全不同的事情.

Adding a hash to a URL indicates you mean to navigate within the page, not navigate to a new page. Clicking the "refresh" button indicates you want to re-request the document. Pressing enter on the URL bar and clicking refresh are very different things.

这篇关于使用#hash从地址栏刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆