json中返回的php中的javascript window.location

i'm trying to put a js window.location within my json output to update a DIV with that javascript code to redirect the user. Only problem here is that my script just doesn't do anything anymore after trying to put the javascript within the json output.

$result = array("error" => false, "html" => null);
$result["error"] = false;
$result["html"] = "<h3>Deel 1 is afgerond, <a href='/shop/?,69'>KLIK HIER</a> om door te gaan naar de begin pagina.</h3>";
$result["html"] .= "<script type="text/javascript"><!-- function Redirect() { window.location="http://www.newlocation.com"; } setTimeout('Redirect()', 10000); //--></script>";

} else {
$result["error"] = true;
$result["html"] = "<h3>Error; Neem contact op met de webmaster</h3>";
}

Now the script dies without doing anything because of this JS.

i just don't know howto format it. I tried changing the " to ' and putting \ before the " but none of it works. anybody ?

Thanks.

Try the following:

$result = array("error" => false, "html" => null);
$result["error"] = false;
$result["html"] = "<h3>Deel 1 is afgerond, <a href='/shop/?,69'>KLIK HIER</a> om door te gaan naar de begin pagina.</h3>";
$result["html"] .= "<script type='text/javascript'>setTimeout(function () { window.location='http://www.newlocation.com'; }, 10000);</script>";

Changes:

  • cleaned up apostrophes/quotes
  • removed unnecessary comment tags
  • changed setTimeout to invoke the function directly rather than via name, which is simpler

There seem to be some issues with this line:

$result["html"] .= "<script type="text/javascript"><!-- function Redirect() { window.location="http://www.newlocation.com"; } setTimeout('Redirect()', 10000); //--></script>";

Change it to:

$result["html"] .= "<script type='text/javascript'> function Redirect() { window.location='http://www.newlocation.com'; } setTimeout('Redirect()', 10000); </script>";