Javascript echo';d由PHP开发';跑不动

Javascript echo';d由PHP开发';跑不动,php,javascript,redirect,echo,Php,Javascript,Redirect,Echo,我对通过php动态响应javascript有点问题。这是我的密码 $url = "http://www.newsite.com"; echo " <html> <head> <title>Redirecting</title> </head> <body onload='redirect()'> Not Logged In <script ty

我对通过php动态响应javascript有点问题。这是我的密码

$url = "http://www.newsite.com";

echo "
    <html>
    <head>
    <title>Redirecting</title>
    </head>
    <body onload='redirect()'>
        Not Logged In

        <script type = 'text/javascript'>
    function redirect() {
        window.location=".$url."
        }
    </script>
    </body>
    </html>
    ";
$url=”http://www.newsite.com";
回声“
重定向
未登录
函数重定向(){
window.location=“.$url.”
}
";
我的javascript控制台告诉我找不到“redirect()”(未捕获引用错误:未定义重定向)


知道是什么原因吗?

你漏掉了一个引号。这将解决您的问题:

function redirect() {
    window.location='".$url."';
}
当前,您的页面呈现如下(请注意缺少引号/语法错误):


你应该把这个函数放在标题区域,然后像这样包装它

echo "
    <html>
    <head>
    <title>Redirecting</title>
    <script type = 'text/javascript'>
    function redirect() {
        window.location='".$url."."'
        }
    </script>
    </head>
    <body onload='redirect()'>
        Not Logged In


    </body>
    </html>
    ";
echo”
重定向
函数重定向(){
window.location='“$url.”
}
未登录
";

完全删除基于客户端的重定向。使用:

header("HTTP/1.0 302 Moved Temporarily"); 
header("Location: $url");
代码有问题

window.location=".$url." 
应该是

window.location=\"".$url."\"

正如@Tomalak所说,您不应该使用javascript来解决这个问题,而应该使用服务器重定向

然而,将php数据转换成javascript有一个更普遍的问题,我将在这里解决这个问题

您需要为javascript和html正确转义
$url
参数。
重定向()
未定义,因为其中存在语法错误

当您需要将javascript数据内联传递到html中时,请使用以下模式。这是最清晰、最安全的方法

<?php
// 1. put all the data you want into a single object
$data = compact($url);
// $data === array('url'=>'http://example.org')

// 2. Convert that object to json
$jsdata = json_encode($url);

// 3. html-escape it for inclusion in a script tag
$escjsdata = htmlspecialchars($jsdata, ENT_NOQUOTES, 'utf-8');
// change utf-8 to whatever encoding you are using in your html.'
// I hope for your sanity you are using utf-8!

// 4. Now assign $escjsdata to a js variable in your html:

?>
<html>
<head>
<title>Redirecting</title>
</head>
<body onload='redirect()'>
    Not Logged In

    <script type = 'text/javascript'>
    function redirect() {
        var data = <?php echo $escjsdata ?>;

        window.location=data.url;
    }
</script>
</body>
</html>

可能是window.location='“$url.”就是这样。我知道这是很愚蠢的:)我会尽快接受你的答案!(显然是5分钟>)我的答案回答了你的问题,但是你应该考虑Tomalak提供的方法。特别是对于重定向,你应该使用页眉方法。如果不是,至少在文档中添加一个链接,以防用户在代码的Grand方案中禁用JavaScript(如by),这是不可能的,因为这将在iFrAME中运行。(我将运行此服务的要求),我需要访问
top
窗口位置,
header
只会影响iframe。但是,是的,我同意他的方法是一种更好的方法,但Javascript现在是必需的。@Esaevian那么,使用
top.window.location.href='“$url。但是,请考虑我的另一点:为已经禁用JavaScript的用户提供链接(<代码> <代码>)。对不起,我没有指定(因为它超出了一般问题的范围)。,但我需要javascript,因为它将在内部和iframe中运行,
头调用只会影响iframe的位置,而我需要修改
顶部
位置(将更新我的js以修改顶部,我将其更改为
窗口
,以确保
顶部
出于某种原因没有问题(可能不是调试的最佳方法…)
<?php
// 1. put all the data you want into a single object
$data = compact($url);
// $data === array('url'=>'http://example.org')

// 2. Convert that object to json
$jsdata = json_encode($url);

// 3. html-escape it for inclusion in a script tag
$escjsdata = htmlspecialchars($jsdata, ENT_NOQUOTES, 'utf-8');
// change utf-8 to whatever encoding you are using in your html.'
// I hope for your sanity you are using utf-8!

// 4. Now assign $escjsdata to a js variable in your html:

?>
<html>
<head>
<title>Redirecting</title>
</head>
<body onload='redirect()'>
    Not Logged In

    <script type = 'text/javascript'>
    function redirect() {
        var data = <?php echo $escjsdata ?>;

        window.location=data.url;
    }
</script>
</body>
</html>