2

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

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

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

Any ideas what's causing this?

1
  • Maybe window.location='".$url."' Commented Dec 2, 2011 at 17:13

5 Answers 5

5

Drop that client-based redirect entirely. Use:

header("HTTP/1.0 302 Moved Temporarily"); 
header("Location: $url");
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I didn't specify (since it's kind of out of the scope of the general question), but I need javascript since this will run inside and iframe, and the header call will only affect the iframe's location, while I need to modify the top location (will update my js to modify top, I changed it to window just to make sure that top wasn't the problem for some reason (perhaps not the best approach to debugging...)
4

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.com;
}

4 Comments

That was it. I know it was something silly like that :) I'll accept your answer as soon as I can! (which is 5 minutes apparently >.>)
My answer answers your question, but you should consider the method as provided by Tomalak. Especially for redirecting, you should use the header method. If not, at least add a link in the document, in case the user has disabled JavaScript (eg by NoScript)
In the grand scheme of the code, that's not possible, since this will be run in an iframe (requirement of the service I will be running this on), and I need to access the top window location, and header will only affect the iframe. But yes, I do agree that his method is a better way, but Javascript is a necessity for now.
@Esaevian Then, use top.window.location.href = '".$url."';. Still, consider my other point: Provide a link (<a href='$url' target='_top'>Redirecting...</a>) for the users who have disabled JavaScript.
2

The code has problem.

window.location=".$url." 

should be

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

Comments

1

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

Comments

0

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 = 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.