javascript location.hash 在 IE 中刷新 [英] javascript location.hash refreshing in IE

查看:22
本文介绍了javascript location.hash 在 IE 中刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要修改散列,在进行某些处理后将其删除,以便在用户刷新时不会导致进程再次运行.

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.

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

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?

更奇怪的行为:哈希值通过重定向来自 Web 应用程序中的其他位置.我发现如果我只是手动添加哈希,将 #myid 添加到 url,它不会重新加载.我是否在已加载的页面上输入哈希值(将 #myid 添加到现有的 url)或在新选项卡中输入完整的 url 都没有关系.

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.

推荐答案

这似乎是 Internet Explorer 的错误(使用 7 和 8 测试).

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

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

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

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

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

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

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)如果您可以控制重定向,您可以用一些 HTML 替换 Location 标头.

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) 如果没有,您可以尝试在页面加载时重新加载页面.为了防止重新加载循环,您可能需要设置一个 cookie.

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.

这篇关于javascript location.hash 在 IE 中刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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