工作原理:
如果用户已经登录,页面会显示,如果用户登录页面,则使用window.location重定向到另一个页面。
问题:
第一页加载页面内容片刻,然后重定向到另一页。我希望它在检查登录状态之前不显示任何内容
什么行不通:
我已经尝试过打开脚本但仍然加载了html。
我不想使用的解决方案:
通过JS包含html,但这会使代码太混乱,很长时间后难以维护..
守则:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
FB.Event.subscribe('auth.statusChange', function(response) {
if(response.status == "connected"){
window.location = "http://www.page.com/Logged.php";
}
});
</script>
<p>Page content</p> // THIS SHOULD WAIT UNTIL THE SCRIPT FINISH
</body>
</html>
SOLUTION:
最好的方法是在登录脚本之后用jquery包含另一个带有html代码的文件。
我添加了一些可以提供帮助的代码:
firstFile.html:
<html>
<head>
<script src="jquery.js"></script>
<script>
$(function(){
$("#includedContent").load("secondFile.html"); //INCLUDE THE SECOND FILE ON THE DIV BELOW
});
</script>
</head>
<body>
<div id="includedContent"></div> //INCLUDE THE SECOND FILE HERE
</body>
</html>
secondFile.html:
<p> This is my include file </p>
答案 0 :(得分:1)
你可以让脚本在运行后打印页面内容吗?你只需要一个单独的文件来获取html,或者将所有html作为字符串放在该脚本中。
编辑: 我会这样做:
<script>
FB.Event.subscribe('auth.statusChange', function(response) {
if(response.status == "connected"){
window.location = "http://www.page.com/Logged.php";
} else {
window.location = "http://www.page.com/NotLogged.php";
});
</script>
听起来你不想这样做。如果你不想使用我给你的脚本,你可以将整个页面放在div标签中,默认情况下隐藏/不可见,你可以在脚本运行后使div可见。