在加载Facebook应用程序时显示GIF

时间:2013-01-25 16:26:00

标签: php facebook jquery

我在Facebook应用的第一页上有很多服务器代码(PHP)。 我等不到用户在等待页面时看到加载动画。

我在这里看到了很多关于如何预加载页面的例子(使用ajax或简单的jQuery),但这是不同的,正如我所说的,页面本身是在服务器上生成的,而在此过程中,仅用户看到一个白色的空白页。

我试图用另一个php页面包装我的主页:

<?php
include_once 'functions.php';
session_start();
$_SESSION['fb'] = new fb();

function phponload(){
    echo
        '<script>
            $(function(){
                $("#mwrapper").load("main.php");
            });
        </script>';
}
?>
<html>
<head>
    <script type="text/javascript">
        function delayer(){
        document.write("<?php phponload();?>");
        }
    </script>
    <link href='styles/default.css' rel='stylesheet' type='text/css' />
        <img class='mainload' src='images/loading.gif'></img>
    <script src='//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js'></script>
    <link rel='stylesheet' type='text/css' href='plugins/clock/clock.css' />
</head>

<body onload='setTimeout("delayer()",1000);'>
    <div id='mwrapper'>

    </div>
</body>

</html>

现在主要的php页面(main.php)有类似的东西:

<!DOCTYPE html>
<html xmlns:fb="http://ogp.me/ns/fb#">
<meta name="google-site-verification"         content="Hyqgg60NTPNA7Q9Z5y9TtezUmwhiEomwZLJDt43Ki2g" />
<!--<img class='mainload' src='images/loading.gif'></img>-->
<?php
include_once 'functions.php';
include 'res/views/getTables.php';
define('TXT_QUESTIONS_NUM', 43);
session_start();
{... more PHP code ... }
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js">  </script>
    <script src='scripts/menuItems.js'></script>
    <script src='scripts/main.js'></script>
    <link href='styles/jquery.ui.smooth.theme.css' rel='stylesheet' type='text/css'/>
    <script type="text/javascript" src="plugins/clock/clock.js"></script>
    <link rel="icon" href="images/favicon.ico">
</head>

<div class='container' id='main'>
...
</div>
</body>
</html>

现在,所有代码都是循环GIF - 但不加载main.php :( 谢谢!

1 个答案:

答案 0 :(得分:1)

Chrome / Firebug控制台上是否有任何错误?

我不明白你为什么在PHP函数中生成加载器JS代码(你可以直接把它放在HTML中),但是这样做你在document.write函数的引号之间放了一个多行块,这会导致错误。

但是,我认为问题在于浏览器将插入的</script>解释为HTML标记的结尾,因此接下来是什么(document.write调用的结束和{{1} } function)被视为HTML而不是Javascript。

编辑:补充答案,这是我用来加载另一页的代码:

delayer

不需要任何PHP生成。此外,function delayer(){ $("#mwrapper").load("main.php", function(){ $('.mainload').remove(); }); } 函数的第二个参数是在加载完成时调用的回调,在这种情况下我用来删除加载GIF。