[英]echo Javascript window.location.href not working
I have a function which echoes javascript to navigate to a different page.我有一个函数可以回显 javascript 以导航到不同的页面。 While navigation occurs, the
当导航发生时,
echo 'window.location.href="'.$url.'";';
does not work and simply prints it on the screen.不起作用,只是将其打印在屏幕上。
"window.location.href="./index.php";
I use my function this way: redirect("./index.php");
我这样使用我的函数:
redirect("./index.php");
My php function is as follows我的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;
}
}
Your browser treats the response as plaintext.您的浏览器将响应视为纯文本。
Prepend to you response a Content-Type: text/html\\
n plus wrap your content inside an <html></html>
tag.为您准备一个
Content-Type: text/html\\
n 加上将您的内容包装在<html></html>
标签中。
Try this way.试试这个方法。
<?php
$yourURL="http://www.stackoverflow.com";
echo ("<script>location.href='$yourURL'</script>");
?>
Why not just use output buffering and not have to deal with JavaScript or meta redirects at all?为什么不直接使用输出缓冲而不用处理 JavaScript 或元重定向呢?
<?php
// Top of your page
ob_start();
// Code goes here
// Redirect
if ($redirect_is_necessary)
{
header('Location: '.$url);
exit;
}
// Rest of page goes here
// Bottom of page
ob_end_flush();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.