最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript echo'd by PHP doesn't run - Stack Overflow

programmeradmin1浏览0评论

I am having a little issue with echoing javascript dynamically via php. Here is my code

$url = "";

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>
    ";

My javascript console is telling me that "redirect()" cant be found (Uncaught ReferenceError: redirect is not defined)

Any ideas what's causing this?

I am having a little issue with echoing javascript dynamically via php. Here is my code

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

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>
    ";

My javascript console is telling me that "redirect()" cant be found (Uncaught ReferenceError: redirect is not defined)

Any ideas what's causing this?

Share Improve this question asked Dec 2, 2011 at 17:11 EsaevianEsaevian 1,7275 gold badges18 silver badges30 bronze badges 1
  • Maybe window.location='".$url."' – Mouse Food Commented Dec 2, 2011 at 17:13
Add a ment  | 

5 Answers 5

Reset to default 5

Drop that client-based redirect entirely. Use:

header("HTTP/1.0 302 Moved Temporarily"); 
header("Location: $url");

You're missing a quotation mark. This will fix your issue:

function redirect() {
    window.location='".$url."';
}

Currently, your page is rendered as follows (note the missing quotes / syntax error):

function redirect() {
    window.location=http://www.newsite.;
}

The code has problem.

window.location=".$url." 

should be

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

You should place that function in the heading area ane wrap it like so.

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>
    ";

As @Tomalak says, you should not be using javascript to solve this problem. Use a server redirect.

However, there is a more general problem with getting php data into javascript. I'll solve that problem here.

You need to escape the $url parameter properly for both javascript AND html. redirect() is not defined because there's a syntax error in it.

Whenever you need to pass javascript data inline into html, use the following pattern. It is the clearest, safest way to do this.

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

// 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>
发布评论

评论列表(0)

  1. 暂无评论