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

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

问题描述

我需要修改哈希,并在发生特定处理后将其删除,以便在用户刷新时不会导致进程再次运行。

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



我似乎无法弄清楚在所有加载完成后更改散列的好方法。而且,与此同时,我们甚至不认为它与加载有关。



关于如何解决这个问题的任何想法?

更奇怪的行为:
哈希是通过重定向从web应用程序的其他位置获得的。我发现如果我只是手动添加散列,将#myid添加到url,它不会重新加载。如果我在已经加载的页面上输入散列(将#myid添加到已存在的url)或在新标签中输入完整的URL,则无关紧要。

解决方案

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

改变window.location.hash不应该导致重载,并且这是一种常用的JavaScript技术,用于维护状态。



如果手动加载页面并使用JavaScript更改哈希值即可使用。



问题出在您重定向到页面时一个不同的位置(即:使用HTTP头位置),然后修改哈希将导致重新加载。

为了解决这个问题,您可以:



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

 < HTML> 
< head>
< meta http-equiv =refreshcontent =0; url = __ REDIRECT_LOCATION __>
< script> window.location =__REDIRECT_LOCATION __;< / script>
< / head>
< / html>

2)如果不是,您可以尝试重新加载页面加载。为防止重载循环,您可能需要设置一个cookie。

  window.location = window.location; // window.location.reload()不起作用。 

在伪代码中:

//如果是Internet Explorer
// if(cookiereloadPerformed未设置)
//设置cookie reloadPerformed=1
//重新加载页面
// else
//清除cookiereloadPerformed

明显的缺点是加载页面会导致两个页面请求&渲染,所以你会希望重新加载是页面加载时的第一件事情。


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.

解决方案

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.

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

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