javascript location.hash 在 IE 中刷新

发布于 2024-08-28 12:36:47 字数 377 浏览 8 评论 0原文

我需要修改哈希值,在进行某些处理后将其删除,这样如果用户刷新,就不会导致进程再次运行。

这在 FF 中工作得很好,但似乎每次我尝试更改哈希值时 IE 都会重新加载。我认为它与页面上加载的其他内容有关,尽管我不确定。我有一个加载的 iframe(与进程相关)以及一些仍在父窗口中获取的脚本。

我似乎无法找到在所有加载完成后更改哈希值的好方法。而且,同时我什至不确定它与负载有关。

关于如何解决这个问题有什么想法吗?

更奇怪的行为: 哈希值通过重定向来自网络应用程序中的其他位置。我发现如果我只是手动添加哈希值,将 #myid 添加到 url,它不会重新加载。如果我在已加载的页面上输入哈希值(将 #myid 添加到已存在的 URL)或在新选项卡中输入完整的 URL,这并不重要。

I need to modify the hash, remove it after certain processing takes place so that if the user refreshes they do not cause the process to run again.

This works fine in FF, but it seems that IE is reloading every time I try to change the hash. I think it is related to other things that are loading on the page, though I am not certain. I have an iframe that loads (related to the process) as well as some scripts that are still being fetched in the parent window.

I can't seem to figure out a good way to change the hash after all the loading completes. And, at the same time am not even positive that it is related to the loading.

Any ideas on how to solve this?

More odd behavior:
The hash is coming from else where in the web app via a redirect. I have found if I simply add the hash by hand, adding #myid to the url, it does not reload. It does not matter if I enter the hash on a page that has already loaded (adding #myid to the already existing url) or by entering the complete url in a new tab.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

英雄似剑 2024-09-04 12:36:47

这似乎是 Internet Explorer 的一个错误(使用 7 和 8 进行测试)。

更改 window.location.hash 不应导致重新加载,并且使用哈希来维护状态是一种常见的 JavaScript 技术。

如果您手动加载页面并使用 JavaScript 更改哈希值,它将起作用。

问题是当您从不同位置重定向到页面(即:使用 HTTP 标头“Location”)时,修改哈希将导致重新加载。

要解决此错误,您可以:

1) 如果您可以控制重定向,则可以用一些 HTML 替换 Location 标头。

<html>
<head>
    <meta http-equiv="refresh" content="0; url=__REDIRECT_LOCATION__">
    <script>window.location = "__REDIRECT_LOCATION__";</script>
</head>
</html>

2)如果没有,您可以尝试在页面加载时重新加载页面。为了防止重新加载循环,您可能需要设置 cookie。

window.location = window.location; // window.location.reload() didn't work.

In pseudo code: 

// if is Internet Explorer
//      if ( cookie "reloadPerformed" is not set )
//          set cookie "reloadPerformed" = "1"
//          reload page
//      else 
//          clear cookie "reloadPerformed"

明显的缺点是加载页面会导致两个页面请求和页面请求。渲染,因此您会希望重新加载成为页面加载时首先执行的操作之一。

This appears to be a bug with Internet Explorer (tested with 7 and 8).

Changing window.location.hash should not result in a reload, and it is a common JavaScript technique to use the hash for maintaining state.

If you manually load a page and change the hash using JavaScript it will work.

The problem is when you are redirected to the page from a different location (ie: using HTTP header "Location"), then modifying the hash will result in a reload.

To get around this bug you could:

1) If you can control the redirect, you could replace the Location header with some HTML.

<html>
<head>
    <meta http-equiv="refresh" content="0; url=__REDIRECT_LOCATION__">
    <script>window.location = "__REDIRECT_LOCATION__";</script>
</head>
</html>

2) if not, you could try reloading the page when it is loaded. To prevent a reload loop you may need to set a cookie.

window.location = window.location; // window.location.reload() didn't work.

In pseudo code: 

// if is Internet Explorer
//      if ( cookie "reloadPerformed" is not set )
//          set cookie "reloadPerformed" = "1"
//          reload page
//      else 
//          clear cookie "reloadPerformed"

The obviously drawback is that loading the page results in two page request & render, so you'll would want the reload to be one of the first things the page does when it loads.

执着的年纪 2024-09-04 12:36:47

@JarneCook 似乎是对的 - 这是 IE 中的一个错误。

您也许可以这样做:

<script type="text/javascript">
  window.location.hash = window.location.hash;
</script>

在页面顶部。在正常情况下,这应该是无操作,但如果用户使用 IE 并且通过重定向到达,则页面将在他们注意到页面已加载之前重新加载。

@JarneCook seems to be right - it is a bug in IE.

You might be able to just do:

<script type="text/javascript">
  window.location.hash = window.location.hash;
</script>

at the top of your page. In normal circumstances, this should be a no-op, but if the user is using IE and has arrived via a redirect, the page will reload before they even notice it has loaded.

甜味拾荒者 2024-09-04 12:36:47

问题是“哈希值是通过重定向来自网络应用程序中的其他位置。”。
如果你使用javascript在客户端中像这样重定向url:

location.href = 'test1.aspx#testhash'

那就可以了!

这就是 IE 的 bug:当 Web 应用程序通过重定向时,浏览器可能只能看到上一个 url,因此当您修改 location.hash 时,浏览器会看到 url 更改,因此刷新页。

The problem is that "The hash is coming from else where in the web app via a redirect.".
If you use javascript to redirect the url in the client like this:

location.href = 'test1.aspx#testhash'

it will be ok !

So this is the IE bug: When a web app via a redirect, the browser may only see the prev url, so when you modify the location.hash, the browser sees a url change, so refreshes the page.

思慕 2024-09-04 12:36:47

我的项目中也存在类似的问题。但我们不能使用上述方法,因为当IE刷新页面时,预加载的数据被重置。
因此,我们使用了浏览器的功能。当您点击“a”标签时,onClick 事件首先发生,事件发生后浏览器使用“href”属性进行重定向。当IE使用href和hash进行重定向时,不存在重新加载。因此,您可以使用 onClick 事件来调用服务器端处理(例如 __doPostBack for asp.net),并且当执行处理时,浏览器将使用“href”属性进行重定向。因此,新页面将不会重新加载。
您还可以在服务器端处理后使用 window.location = yourNewLocationWithHash 调用。
我希望这有帮助 =)

The similar issue existed in my project. But we could not use methods described above, because when IE refreshed a page, preloaded data was reset.
So, we used the feature of the browser. When you click for 'a' tag, onClick event happened firstly and after event browser use 'href' attribute for redirecting. When IE use href with hash for redirecting, reloading do not exist. So, you can use onClick event for invoke server-side processing(__doPostBack for asp.net for example) and when the processing will be executed, browser will use 'href' attribute for redirecting. So, new page will not be reloaded.
Also you can use window.location = yourNewLocationWithHash invoking after server-side processing.
I hope this help =)

慢慢从新开始 2024-09-04 12:36:47

面临这个问题,正如其中一个答案所建议的,问题仅在 302/301 重定向时出现。如果页面不是重定向,则哈希更改不会重新加载。我正在使用 PHP 进行重定向,并且不想使用 cookie 来停止重定向。

更重要的是这个问题在一些IE9浏览器中也存在,尝试了5个IE9浏览器,4个重新加载了页面。

这是在头部添加的修复:

<!--[if lt IE 10]>
    <script type="text/javascript">
        if(window.location.hash.replace('#','').length > 0
            && window.location.hash.search('stopredirectioninie') == -1)
        {
            window.location.href = window.location.href+'&stopredirectioninie';
        }
    </script>
<![endif]-->

Was facing this issue, As suggested in one of the answers, the issue was only when a 302/301 redirection. Hash change does not reload if the page was not a redirect. I was redirecting using PHP and did not want to use a cookie to stop the redirection.

More over this issue was there in some IE9 browsers as well, tried 5 IE9 browsers, 4 reloaded the page.

Here is a fix added this in head section :

<!--[if lt IE 10]>
    <script type="text/javascript">
        if(window.location.hash.replace('#','').length > 0
            && window.location.hash.search('stopredirectioninie') == -1)
        {
            window.location.href = window.location.href+'&stopredirectioninie';
        }
    </script>
<![endif]-->
许一世地老天荒 2024-09-04 12:36:47

这是一个跨浏览器的解决方案。适用于 IE、Chrome、Safari 和 FF(已尝试使用最新版本)。

var pos = location.href.indexOf('c=');
location = (pos < 0 ?
                    location + (location.href.indexOf('?') < 0 ? '?' : '&')
                    : location.href.substring(0, pos))
           + 'c=' + Math.floor(Math.random()*11) + '#' + comment_id ;

基本上,我利用查询(“?”)字符串来触发带有哈希值的页面重新加载。第一行的作用是检查是否存在我们的“黄金”查询字符串(我使用代表“注释”的“c”变量)。如果有,

  1. 新的 URL 将包含“c=”之前的所有内容;
  2. 然后添加我们的黄金“c =”+0到10之间的随机数+“#”+我的评论ID,浏览器重新加载时需要跳转到该评论ID。

如果没有,

  1. 新的 URL 将拥有旧 URL 曾经拥有的一切;
  2. 如果旧的 URL 已经包含一些其他查询字符串(“?”之后的内容),请添加查询追加运算符“&”;
  3. 如果没有“?”,则添加;
  4. 然后是上面提到的“黄金”查询。

我在“?”后面添加随机数的原因是在第一次重新加载后出现类似“?#comment-10”的内容。在这种情况下,下次对 URL 的更改将不会重新加载页面,因为浏览器将其理解为锚点跳转指令。

为了强制重新加载,我们需要在查询中添加一些随机的东西,以便新的 URL 与以前的不同。

该解决方案适用于所有浏览器,并确保重新加载不会破坏现有查询。唯一的注意是确保您的“黄金”查询变量名称是唯一的。

希望这有帮助。

Here is a cross-browser solutions. Works in IE, Chrome, Safari, and FF (tried with latest versions).

var pos = location.href.indexOf('c=');
location = (pos < 0 ?
                    location + (location.href.indexOf('?') < 0 ? '?' : '&')
                    : location.href.substring(0, pos))
           + 'c=' + Math.floor(Math.random()*11) + '#' + comment_id ;

Basically, I leverage query ("?") string to trigger the page reload with hash. What the first line does is it checkes whether there is our "golden" query string (I use "c" variable which stands for "comment"). If there is,

  1. the new URL will have everything before that "c=";
  2. then adds our golden "c=" + a random number between 0 and 10 + "#" + my comment id to which the browser needs to jump when reloaded.

If there is no,

  1. the new URL will have everything old URL used to have;
  2. if the old URL already contains some other query string (something after "?"), add a query append operator "&";
  3. if no "?", then add it;
  4. then goes the above mentioned "golden" query.

The reason I add a random number after "?" is that after the first reload there is something like "?#comment-10". In this case, the next change to the URL will not reload the page, since browser understands it as an anchor jump instruction.

To force reload, we need to add some random thing to the query so that the new URL is different from the previous.

This solution will work on all browsers and will make sure the reload doesn't break the existing query. The only note is to make sure your "golden" query variable name is unique.

Hope this helps.

太傻旳人生 2024-09-04 12:36:47

我们有同样的问题。

在我们的例子中,它由一个 http URL 组成,该 URL 被 Apache 重定向到 https。由于哈希符号后面的字符串从未传递到服务器,因此它丢失了。

We had the same problem.

In our case it consisted of a http URL which was redirected to https by Apache. Since the string after the hash sign is never passed to the server, it got lost.

七堇年 2024-09-04 12:36:47

如果您使用 javascript 设置哈希值,请勿使用“#”

window.location.hash = '#foo'; //IE will reload the page
window.location.hash = 'foo'; //IE will set the hash but will not reload the page

If you use javascript to set the hash don't use a "#"

window.location.hash = '#foo'; //IE will reload the page
window.location.hash = 'foo'; //IE will set the hash but will not reload the page
遇见了你 2024-09-04 12:36:47

在我看来,如果你改变哈希值,你基本上就是在改变页面的位置,所以 IE(或任何浏览器)都会重新加载。你打算如何做到这一点? window.location.hash = ""; ?

也许 Firefox 足够聪明,可以看到您在做什么并避免刷新。

It seems to me that if you change the hash, you are basically changing the location of the page, and so IE (or any browser) would reload. How are you trying to do this? window.location.hash = ""; ?

Maybe Firefox is just smart enough to see what you are doing and avoid the refresh.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文