duanbin4847 2010-07-07 19:20
浏览 63
已采纳

为什么这个javascript不起作用?

It's been really frustrating me i have this code:

<script type ="text/javascript">
scriptAr = new Array(); // initializing the javascript array

<?php

foreach ($docsannlist as $subdocs) 
{
    $lines = $subdocs; // read file values as array in php
    $count = count($subdocs); //this gives the count of array
    //In the below lines we get the values of the php array one by one and update it in the script array.
    foreach ($lines as $line)
    {
        print "scriptAr.push(\"$line\");
"; // This line updates the script array with new entry
    }
}

?>

document.write(scriptAr);
</script>

and for some reason its just not working. Please help!

  • 写回答

3条回答 默认 最新

  • duaj39673 2010-07-07 19:38
    关注

    The problem is probably not in your javascript code.

    My guess is that you have string escaping problems. Try to use addslashes php function on the $line variable before printing.

    // as simple as that
    $line = addslashes($line);
    

    Because if a line has a quote your PHP will work fine but your javascript will look like this:

    scriptAr.push("some text here "a quotation" and some other text");
    

    Which is invalid syntax.

    If you use addslashes, the line will become:

    scriptAr.push("some text here \"a quotation\" and some other text");​​
    

    Which will run just fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程