为什么 window.location 在 FF 中比在 Chrome 中快得多?

Why is window.location way faster in FF than in Chrome?

我正在使用此代码根据他们选择的浏览器语言将人们重定向到某些 URL。

如您所见,如果他们有德语浏览器,则不会发生任何变化。 如果他们有英语、法语或西班牙语浏览器,他们将被重定向到正确的 URL。

这只会在浏览器会话中发生一次,因此如果需要,他们可以使用另一种语言访问德语家庭。

<?php 

session_start();
if(!isset($_SESSION['visitedOnce'])){

    if(is_home() && ICL_LANGUAGE_CODE == 'de' ){

        $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        switch ($lang){
            case "de":
                //echo "PAGE DE";
                break;
            case "en":
                //echo "PAGE EN";
                echo '<script type="text/javascript">window.location = "http://www.google.com";</script>';
                break;
            case "es":
                //echo "PAGE ES";
                echo '<script type="text/javascript">window.location = "http://www.google.es";</script>';
                break;        
            case "fr":
                //echo "PAGE FR";
                echo '<script type="text/javascript">window.location = "http://www.google.fr";</script>';
                break;        
            default:
                //echo "PAGE EN - Setting Default";
                echo '<script type="text/javascript">window.location = "http://www.google.com";</script>';
                break;
        }

    }

    $_SESSION['visitedOnce'] = true;
}
?>

在 Firefox 中运行正常,HTML 不会加载,重定向会无缝进行。

但是,Google Chrome 在重定向发生之前显示了丑陋的无样式块 HTML。

有没有办法使用这种 javascript 重定向来阻止它?

谢谢!

However, Google Chrome shows ugly chunks of unstyled HTML before the redirect occur. Is there a way to prevent it using this kind of javascript redirect?

让您的 html 文档看起来像这样

<body style="display:none;">
...
<script>
    document.body.style.display = '';
</script>
</body>

那么在 PHP 中进行重定向呢?然后你将看不到无样式的丑陋块 HTML... 请注意,你在 header('Location: http://www.google.***');.

之前没有输出
<?php 
session_start();

if(!isset($_SESSION['visitedOnce'])){
    $_SESSION['visitedOnce'] = true;

    if(is_home() && ICL_LANGUAGE_CODE == 'de' ){
        $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        switch ($lang){
            case "de":
                //echo "PAGE DE";
                break;
            case "en":
                //echo "PAGE EN";
                header('Location: http://www.google.com');
                exit;
                //echo '<script type="text/javascript">window.location = "http://www.google.com";</script>';
                //break;
            case "es":
                //echo "PAGE ES";
                header('Location: http://www.google.es');
                exit;
                //echo '<script type="text/javascript">window.location = "http://www.google.es";</script>';
                //break;        
            case "fr":
                //echo "PAGE FR";
                header('Location: http://www.google.fr');
                exit;
                //echo '<script type="text/javascript">window.location = "http://www.google.fr";</script>';
                //break;        
            default:
                //echo "PAGE EN - Setting Default";
                header('Location: http://www.google.com');
                exit;
                //echo '<script type="text/javascript">window.location = "http://www.google.com";</script>';
                //break;
        }

    }

    //$_SESSION['visitedOnce'] = true;
}
?>