Facebook Callback appends '#_=_' to Return URL

前端 未结 23 2082
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 14:56

Facebook callback has started appending #_=_ hash underscore to the Return URL

Does anyone know why? What is the solution?

23条回答
  •  旧巷少年郎
    2020-11-22 15:07

    TL;DR

    if (window.location.hash === "#_=_"){
        history.replaceState 
            ? history.replaceState(null, null, window.location.href.split("#")[0])
            : window.location.hash = "";
    }
    

    Full version with step by step instructions

    // Test for the ugliness.
    if (window.location.hash === "#_=_"){
    
        // Check if the browser supports history.replaceState.
        if (history.replaceState) {
    
            // Keep the exact URL up to the hash.
            var cleanHref = window.location.href.split("#")[0];
    
            // Replace the URL in the address bar without messing with the back button.
            history.replaceState(null, null, cleanHref);
    
        } else {
    
            // Well, you're on an old browser, we can get rid of the _=_ but not the #.
            window.location.hash = "";
    
        }
    
    }
    

    Step by step:

    1. We'll only get into the code block if the fragment is #_=_.
    2. Check if the browser supports the HTML5 window.replaceState method.
      1. Clean the URL by splitting on # and taking only the first part.
      2. Tell history to replace the current page state with the clean URL. This modifies the current history entry instead of creating a new one. What this means is the back and forward buttons will work just the way you want. ;-)
    3. If the browser does not support the awesome HTML 5 history methods then just clean up the URL as best you can by setting the hash to empty string. This is a poor fallback because it still leaves a trailing hash (example.com/#) and also it adds a history entry, so the back button will take you back to #_-_.

    Learn more about history.replaceState.

    Learn more about window.location.

提交回复
热议问题