这是我第一次使用php。我试图在一个网站中优化js,我看到headscript()
附加js文件。
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js');
在这里我试图添加一个附加脚本标签的属性
$this->headScript()->setAllowArbitraryAttributes(true);
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js',
我也试过了
$this->headScript()->setAllowArbitraryAttributes(true);
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js',$attrs = array("async" => "true"))
array(“async”=>“true”));
文件中的php部分
<?php
$this->headScript()->setAllowArbitraryAttributes(true);
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', $attrs = array("async" => "true"));
?>
我期待输出
<script async="true" src="/js/image-gallery/jquery.simplemodal.1.4.4.min.js"></script>
相反,我得到了
<script type="Array" src="/js/image-gallery/jquery.simplemodal.1.4.4.min.js"></script>
怎么解决?我无法找到任何通过headscript();
添加属性的样本
您需要将属性作为第三个参数传递
$this->headScript()->appendFile(
'/js/image-gallery/jquery.simplemodal.1.4.4.min.js',
null,
array('async' => 'true', 'foo' => 'bar')
);
null
这里是'type'属性,默认为text/javascript
看起来你正在使用Zend
框架。如果是这样,请参考HeadScript Helper
您需要将属性作为$attrs = array()
传递
appendFile($src, $type = 'text/javascript', $attrs = array())
所以你的代码应该是
更新的代码
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', $type = 'text/javascript', $attrs = array("async" => "true"));
代替
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', array("async" => "true"));
默认情况下禁用任意属性。要允许此类属性,可以通过setAllowArbitraryAttributes()方法启用它们:
$this->headScript()->setAllowArbitraryAttributes(true);
然后
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', null, array("async" => "true"));