dsdqpdjpq16640651 2011-06-02 17:57
浏览 22
已采纳

too long

Here is the code I have,

<script language="JavaScript">
    var url = "http://localhost:8888/uploads/"+<? $name ?>+"/output.txt";
    setTimeout("top.location.href = url",1000);
</script>

$name is a PHP variable which has the name of which directory I'm trying to redirect to.

Needless to say, it isn't working. What's wrong with it? I don't know a lot about javascript so I probably did something stupid

Thanks

  • 写回答

7条回答 默认 最新

  • dongziya9863 2011-06-02 18:00
    关注

    If your mixing PHP with JavaScript, it's always advisable to check the output being sent to the browser: right click on your website and click view source!

    JavaScript doesn't care whether the content being sent to it is static HTML, from a Database or generated by PHP. If its in the output, it'll parse it.

    If you'd have done that, you'd notice that your not echo'ing the $name variable.

    <script language="JavaScript">
        var url = "http://localhost:8888/uploads/"+<? echo $name ?>+"/output.txt";
        setTimeout("top.location.href = url",1000);
    </script>
    

    But that'd give you

    <script language="JavaScript">
        var url = "http://localhost:8888/uploads/"+ foo +"/output.txt";
        setTimeout("top.location.href = url",1000);
    </script>
    

    Which isn't valid JavaScript, as foo is now a JS variable, not a string.

    So you should have:

    <script language="JavaScript">
        var url = "http://localhost:8888/uploads/<? echo $name ?>/output.txt";
        setTimeout("top.location.href = url",1000);
    </script>
    

    Furthermore, passing a string to setTimeout (or setInterval) is not recommend; for the same reasons against using eval(), so you should end up with something like this instead:

    <script language="JavaScript">
        var url = "http://localhost:8888/uploads/<? echo $name ?>/output.txt";
        setTimeout(function () {
            top.location.href = url
        },1000);
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭
  • ¥15 Matlab读取根元素出错