Javascript headScript()->;appendFile的行为与headScript()相同->;来自视图的前置文件
我遇到了一些问题。从视图看,appendFile似乎不起作用。如果我将其更改为prependFile,则它具有相同的行为 layout.phtmlJavascript headScript()->;appendFile的行为与headScript()相同->;来自视图的前置文件,javascript,zend-framework,Javascript,Zend Framework,我遇到了一些问题。从视图看,appendFile似乎不起作用。如果我将其更改为prependFile,则它具有相同的行为 layout.phtml <!doctype html> <html lang="en" dir="ltr"> <head> <?php $this->headScript()->appendFile('http://code.jquery.com/ui/1.10.3/jquery-
<!doctype html>
<html lang="en" dir="ltr">
<head>
<?php
$this->headScript()->appendFile('http://code.jquery.com/ui/1.10.3/jquery-ui.js');
$this->headScript()->appendFile('/theme/javascripts/application.js');
$this->headScript()->appendFile('/js/own.js');
?>
</head>
<body>
<?php echo $this->layout()->content; ?>
<?php echo $this->headScript() ?>
</body>
</html>
index.phtml
<?php $this->headScript()->appendFile('/js/another.js') ?>
输出
<!doctype html>
<html lang="en" dir="ltr">
<head>
</head>
<body>
<script type="text/javascript" src="/js/another.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript" src="/theme/javascripts/application.js"></script>
<script type="text/javascript" src="/js/own.js"></script>
</body>
</html>
如您所见,/js/other.js将是第一个js。那不是我想要的,我想把它放在最后。有人知道怎么了吗
/js/other.js
位于开头,因为首先调用布局.phtml
,然后在调用
时调用索引.phtml
文件
现在,在您的代码中有:
因此,它实际上调用了layout()
,因此调用了index.phtml
,然后调用了headscript()
,其中包含其余部分。它附加index.phtml
中提到的js
,然后附加headscript()
中的其余部分。试试另一种方法
你应该试试:
<!doctype html>
<html lang="en" dir="ltr">
<head>
<?php
$this->headScript()->appendFile('http://code.jquery.com/ui/1.10.3/jquery-ui.js');
$this->headScript()->appendFile('/theme/javascripts/application.js');
$this->headScript()->appendFile('/js/own.js');
?>
</head>
<body>
<?php echo $this->headScript() ?>
<?php echo $this->layout()->content; ?>
</body>
</html>
理想情况下,您应该在
部分中输出所有脚本。视图脚本在布局之前呈现,因此当您附加/js/other.js
时,没有其他脚本,这就是为什么布局稍后添加的脚本会在它之后结束
通过将布局中的所有appendFile()
调用更改为prependFile()
,您应该能够实现所需的顺序。(您可能需要颠倒顺序,因为您现在正在准备。)然后执行顺序应该是:
- 查看脚本附录
/js/other.js
- 布局预先添加
/js/own.js
- 布局预先添加
/theme/javascripts/application.js
- 布局前置<代码>http://code.jquery.com/ui/1.10.3/jquery-ui.js
也可以考虑使用内联脚本帮助器(它以同样的方式工作),因为在“代码> >代码中有头脚本输出脚本可能会混淆未来开发代码的开发人员。
我想把它放在体标签上进行速度优化。如果你想在最后加载所有的JS,然后保持代码不变,并在索引中将
更改为
。视图脚本在布局之前呈现。否则,您根本无法从视图脚本向
添加JS。