douzai1074 2014-12-04 11:21
浏览 68
已采纳

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.

  • 写回答

2条回答 默认 最新

  • donglu953744 2014-12-04 11:24
    关注

    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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探