duaiwo9093 2016-03-04 18:31
浏览 34
已采纳

ZF2 headScript包含两次相同的文件

I am using ZF2 and facing a weird behavior. When I use the headScript to append or prepend some Javascript file, the file is included twice. The same happens with inlineScript.

This is my actual view with the headScript()->appendFile():

<?php echo $this->headScript()->appendFile($this->basePath('js/dashboard.min.js')); ?>

With this implementation, when my page is loaded, all the requests are made twice and this is very bad cause are async requests to an API:

Test
[2, 1, 0, 0, 0, 0, 0]
Test
[2, 1, 0, 0, 0, 0, 0]

I tried also without the $this->basePath():

<?php echo $this->headScript()->appendFile('js/dashboard.min.js'); ?>

And got the same behavior.

But if I put this Javascript without the headScript, it just works fine, include once.

<script src="js/dashboard.min.js"></script>

// And the output
Test
[2, 1, 0, 0, 0, 0, 0]

What I can see is that the first include is made when page is loaded and the second some seconds after (2 or 3 seconds).

Someone already faced this behavior? If yes, Did Solve it? How?

  • 写回答

1条回答 默认 最新

  • douyuepi6485 2016-03-04 18:44
    关注

    I believe the problem is that you are echoing out the headScript. Try removing your echo statement, and I believe it will work fine.

    Use this:

    <?php $this->headScript()->appendFile($this->basePath('js/dashboard.min.js')); ?>
    

    Instead of this:

    <?php echo $this->headScript()->appendFile($this->basePath('js/dashboard.min.js')); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了