我有一个函数,它回声JavaScript以导航到不同的页面。导航发生时,回声JavaScript window.location.href不工作
echo 'window.location.href="'.$url.'";';
不起作用,只是将其打印在屏幕上。
"window.location.href="./index.php";
我用我的功能是这样的:redirect("./index.php");
我的PHP函数如下
function redirect($url)
{
if (!headers_sent())
{
header('Location: '.$url);
exit;
}
else
{
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
}
}
你的$网址应该在情况下,它包含了”,反正是页面的html页面?浏览器可能会训释它以纯文本格式正确转义。此外,作为一个有点迂腐,HTTP规范使然 – Corbin 2012-04-11 18:52:32
这是一个php页面,我的项目需要我给出相对URL,因此我没有选择这个 – zed 2012-04-11 19:12:22
一个PHP页面并不意味着它会被自动解释为HTML – Corbin 2012-04-11 19:13:22