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:

[2, 1, 0, 0, 0, 0, 0]
[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
[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')); ?>
    解决 无用
    打赏 举报

相关推荐 更多相似问题