This is my first time working with php. I was trying to optimize js in a website where I saw headscript()
to append js file.
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js');
In this I am trying to add an attribute the script tag appended
$this->headScript()->setAllowArbitraryAttributes(true);
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js',
I also tried
$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 portion in the file
<?php
$this->headScript()->setAllowArbitraryAttributes(true);
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', $attrs = array("async" => "true"));
?>
I expected output to be
<script async="true" src="/js/image-gallery/jquery.simplemodal.1.4.4.min.js"></script>
Instead I got
<script type="Array" src="/js/image-gallery/jquery.simplemodal.1.4.4.min.js"></script>
How to solve it? I was not able to find any samples where attributes are added via headscript();
You need to pass the attributes as the third argument
$this->headScript()->appendFile(
'/js/image-gallery/jquery.simplemodal.1.4.4.min.js',
null,
array('async' => 'true', 'foo' => 'bar')
);
null
here is the 'type' attribute, which will default to text/javascript
Looks like you are using Zend
framework. If so please refer HeadScript Helper
You need to pass the attributes as $attrs = array()
appendFile($src, $type = 'text/javascript', $attrs = array())
So your code should be
UPDATED CODE
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', $type = 'text/javascript', $attrs = array("async" => "true"));
instead of
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', array("async" => "true"));
Arbitrary attributes are disabled by default. To allow such attributes, you can enable them via the setAllowArbitraryAttributes() method:
$this->headScript()->setAllowArbitraryAttributes(true);
Then
echo $this->headScript()->appendFile('/js/image-gallery/jquery.simplemodal.1.4.4.min.js', null, array("async" => "true"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.