从框架中的脚本重定向页面



我使用javascript每4秒(lobby_box.php(加载一个页面。此页面正在我的(索引.php(页面上的div 中加载。问题是(lobby_box.php(包含一个代码,该代码应该从索引.php>另一个页面.php重定向客户端。但它会将客户端重定向到每 4 秒加载 (lobby_box.php( 的div 内部(另一个页面.php(。

所以基本上,它不会重定向整个页面,只是重定向div 内部。

我已经尝试使用我能得到的一切。我一直在尝试使用javascript,jquery,php代码进行重定向,但它仍然只在div内部重定向。

function getLobbyBox() { 
    var http; 
    if (window.XMLHttpRequest) { 
        http = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
        http = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    http.onreadystatechange=function() 
    { 
        if (http.readyState==4 && http.status==200) 
        { 
            document.getElementById("lobby_new_content").innerHTML= http.responseText; 
        } 
    }   
    http.open("GET", "includes/lobby_box.php?noCache=" + Math.random(), true); 
    http.send(); 
} 

这是每 4 秒加载 (lobby_box.php( 一次的函数。

这是应该重定向到另一个页面的代码.php但只能在div 中重定向到它。

if(mysqli_num_rows($game) > 0) {
 echo '<script>window.location.href = "/anotherpage.php";</script>';
 exit();
}

我也试过

if(mysqli_num_rows($game) > 0) {
 header('location: anotherpage.php');
 exit();
}

甚至有可能像那样解决吗?还是我需要找到另一种方式?

假设您的页面(父页面和 iframe 中的页面(位于同一域中,您可以轻松地从框架调用window.parent并以这种方式重定向。

window.parent.location.href = '/whatever-page';
<</div> div class="one_answers">

问题已解决。

可能是因为它不是真正的框架,而是使用 XMLHttpRequest 加载到div 中的内容。工作代码是。

<META http-equiv="refresh" content="0;URL=/anotherpage.php" target="_new">

我只是把它放在我的 if 语句中的回声中。

最新更新