javascript location.hash refreshing in IE

后端 未结 9 1211
既然无缘
既然无缘 2020-11-28 06:03

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

相关标签:
9条回答
  • 2020-11-28 06:37

    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.

    0 讨论(0)
  • 2020-11-28 06:43

    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.

    0 讨论(0)
  • 2020-11-28 06:43

    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 =)

    0 讨论(0)
提交回复
热议问题