Skip to content

Manual change of hash into the URL doesn't trigger the route in IE11 #1849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Aymkdn opened this issue Oct 26, 2017 · 16 comments · May be fixed by #1988
Open

Manual change of hash into the URL doesn't trigger the route in IE11 #1849

Aymkdn opened this issue Oct 26, 2017 · 16 comments · May be fixed by #1988
Labels

Comments

@Aymkdn
Copy link

Aymkdn commented Oct 26, 2017

Version

3.0.1

Reproduction link

https://codepen.io/Aymkdn/pen/MENZPx

Steps to reproduce

  1. Use IE11
  2. Open codepen at https://codepen.io/Aymkdn/pen/MENZPx
  3. Click on "Open demo"
  4. Click on "Go to foo" or click on "Go to bar" to see it works correctly
  5. Manually change the hash into the URL from the IE11 address bar (for example replace "#/foo" with "#/bar")

What is expected?

The component to be loaded based on the current hash into the URL

What is actually happening?

The changes are not detected under IE11. It works with Firefox.


When the page is already opened and I want to copy/paste a link into the same page which has the same url but a different path/hash

@posva posva added the ie label Oct 30, 2017
@Aymkdn
Copy link
Author

Aymkdn commented Oct 31, 2017

In the meantime, I use this workaround:

if ('-ms-scroll-limit' in document.documentElement.style && '-ms-ime-align' in document.documentElement.style) { // detect it's IE11
  window.addEventListener("hashchange", function(event) {
    var currentPath = window.location.hash.slice(1);
    if (store.state.route.path !== currentPath) {
      router.push(currentPath)
    }
  }, false)
}

@arcadeJHS
Copy link

Thanx for the suggestion!

@yuxizhe
Copy link

yuxizhe commented Nov 10, 2017

same problem when using location.href='...'

learn from @Aymkdn

if (checkIE()) {
      window.addEventListener('hashchange', () => {
        var currentPath = window.location.hash.slice(1)
        if (this.$route.path !== currentPath) {
          this.$router.push(currentPath)
        }
      }, false)
   }

@Aymkdn
Copy link
Author

Aymkdn commented Nov 17, 2017

Where i have to put the code example above?
Anywhere after your Vue script. It's an "independant" code, but you need to make sure router and store are already defined.

If you use the code from @yuxizhe I think it's added into the mounted of the Vue.

@margokopli
Copy link

I think this is related to IE issue #3740423 https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/3740423/

@ghost
Copy link

ghost commented Feb 15, 2018

I also have something similar happening to me in IE and also in FF but all good in Safari and Chrome.

I'm having a problem with router.push not appending the correct locale in the url for example: mydomain.com/en. I have a Store set up in Nuxt and a plugin with my logic. I also have nuxt-i18n module installed:

** My Store Logic **

import Vuex from 'vuex'

const store = () => new Vuex.Store({

    state: {
        locales: ['en', 'de', 'cn', 'es', 'pt'],
    },

    mutations: {

        setLocale(state, locale) {
            if (state.locales.indexOf(locale) !== -1) {
                this.app.i18n.locale = locale;
            }
 

My Plugins logic with various console logs to output data:

export default async ({ app, store, commit, route }) => {

    // Only run on client-side
    if (process.server) return;

    console.log('BEFORE MUTATION: ' + app.i18n.locale);  // 'en' is default
    app.router.push(app.i18n.locale) ; // Works and appends to url '/en'. Works in all browsers
    store.commit('setLocale', 'es');  // Manually setting to es
    console.log('AFTER MUTATION: ' + app.i18n.locale); // es
    app.router.push(app.i18n.locale);  // Does not work in FF or IE resolves to '/' after mutation. Works fine in Chrome

}

Why would router.push not function like expected after passing in app.i18n.locale after it's been updated/mutated?

I also passed into the 2nd and 3rd arguments for router.push to see what's going on:

await store.commit('setLocale', 'es'); // Manually setting to es
    console.log('AFTER MUTATION: ' + app.i18n.locale); // es
    await app.router.push(
        app.i18n.locale,
        console.log('onComplete: ' + app.i18n.locale), //onComplete: es
        console.log('onAbort: ' + app.i18n.locale) // onAbort: es
    ); 

@zanedev
Copy link

zanedev commented Mar 26, 2018

@andrade1379 did you ever figure this out? I'm having a similar issue where somes routes aren't loading on FF or IE but everything else is fine.

@aj-flynn
Copy link

Using the workaround by @Aymkdn I ran into 2 issues. Where to put the code and I had an issue that required me to press the browser back button twice. I used mounted() to add the event handler and changing from .push to .replace fixed the back button issue.

new Vue({
    router: Router,
    mounted() {
        var app = this;

        if ("-ms-scroll-limit" in document.documentElement.style && "-ms-ime-align" in document.documentElement.style) { 
            window.addEventListener("hashchange",
                function () {
                    var currentPath = window.location.hash.slice(1);
                    if (app.$route.path !== currentPath) {
                        app.$router.replace(currentPath);
                    }
                },
                false);
        }
    }
});

@binhphanduc
Copy link

I faced the same issue. And also, hashchange didn't trigger when changing url by clicking on url.

@zifnab87
Copy link

zifnab87 commented Oct 8, 2018

This still happened for me with version vue 2.5.17, vue-router 3.0.1 - I used this code (with minor changes from @yuxizhe and @Aymkdn) in created() - it should work in mounted() too:

let app = {
    data: function () {
    },
    methods: {

    },
    mounted () {

    },
    created() {
        if ('-ms-scroll-limit' in document.documentElement.style 
            && '-ms-ime-align' in document.documentElement.style) { // detect it's IE11
            window.addEventListener("hashchange", (event) => {
                var currentPath = window.location.hash.slice(1);
                if (this.$route.path !== currentPath) {
                    this.$router.push(currentPath)
                }
            }, false)
        }
   }
}

@zifnab87
Copy link

zifnab87 commented Oct 8, 2018

I faced the same issue. And also, hashchange didn't trigger when changing url by clicking on url.

I had the same issue with one of my links - I was using <a href="/#/..."> instead of router-link. Is it your case too?

@rickysang
Copy link

Same problem in old version Chrome. I tested in Chrome v52.

@Krabi

This comment has been minimized.

@vuejs vuejs deleted a comment from CodeLittlePrince Mar 5, 2019
@vuejs vuejs deleted a comment from shadowZs Mar 5, 2019
@vuejs vuejs deleted a comment from Jarzka Mar 5, 2019
@vuejs vuejs deleted a comment from arcadeJHS Mar 26, 2019
@posva posva removed the has PR label Jul 3, 2019
@vuejs vuejs deleted a comment Jul 3, 2019
@tje3d

This comment has been minimized.

@tje3d

This comment has been minimized.

@posva

This comment has been minimized.

druid0523 pushed a commit to druid0523/vue-router that referenced this issue Sep 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.