javascript 为什么 window.location.href= 不使用 Safari 转发到页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31223216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:29:32  来源:igfitidea点击:

Why isnt window.location.href= not forwarding to page using Safari?

javascriptparse-platformfacebook-javascript-sdk

提问by Dano007

My site lets users login via the Fb button, I'm using the FB / Parse.com JDK for this https://parse.com/docs/js/guide#users-facebook-users

我的网站允许用户通过 Fb 按钮登录,我使用 FB / Parse.com JDK 用于此https://parse.com/docs/js/guide#users-facebook-users

Once the user has been identified, the below code logs the user in and forwards them onto a url. This works as expected under Chrome, but will not work using Safari, the page just stays on the fb.html page which is blank

一旦用户被识别,下面的代码就会让用户登录并将他们转发到一个 url。这在 Chrome 下按预期工作,但在使用 Safari 时不起作用,页面只停留在空白的 fb.html 页面上

I've seen that there were some historic issues with

我已经看到有一些历史问题

window.location.href=

window.location.href=

But, can't find a fix that works for my solution. Does anyone know a way around this?

但是,找不到适用于我的解决方案的修复程序。有谁知道解决这个问题的方法?

Parse.FacebookUtils.logIn(null, {
    success: function(user) {
        if (!user.existed()) {

        } else {
            window.location.href="user_home.html";

        }
    },
    error: function(user, error) {

    }
});

回答by stdob--

Best way work in all browsers:

在所有浏览器中工作的最佳方式:

setTimeout(function(){document.location.href = "user_home.html";},250);

回答by Merb

I had this happening to me as well on safari and found this post but found another solution I wanted to add with lots of browser support. Instead of replacing the current location use the method that is on the location object called assign()

我在 safari 上也遇到了这种情况并找到了这篇文章,但找到了另一个解决方案,我想添加很多浏览器支持。不是替换当前位置,而是使用位于名为 assign() 的位置对象上的方法

document.location.assign(document.location.origin + "/user_home.html")

This also works

这也有效

location.assign(location.origin + "/user_home.html")

Tested in Chrome and safari on desktop and mobile iOS devices

在桌面和移动 iOS 设备上的 Chrome 和 safari 中测试

reference: https://www.w3schools.com/jsref/met_loc_assign.asp

参考:https: //www.w3schools.com/jsref/met_loc_assign.asp

回答by Matthew

I think you need to use...

我认为你需要使用...

window.location = 'user_home.html';

回答by Ivijan Stefan Stipi?

When I stack to this problem, I made function what working well on any Safari and also all browsers including mobile browsers:

当我解决这个问题时,我使函数在任何 Safari 以及所有浏览器(包括移动浏览器)上运行良好:

function windowLocation(url){
    var X = setTimeout(function(){
        window.location.replace(url);
        return true;
    },300);

    if( window.location = url ){
        clearTimeout(X);
        return true;
    } else {
        if( window.location.href = url ){
            clearTimeout(X);
            return true;
        }else{
            clearTimeout(X);
            window.location.replace(url);
            return true;
        }
    }
    return false;
};

Is a bit "dirty" solution but give you ability to redirect your page in any case.

有点“脏”的解决方案,但让您能够在任何情况下重定向您的页面。