Add attribute attributes while appending a js file using headscript()

inlinescript
zendview helper
zend framework tutorial
laminas

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

HeadScript Helper - Manual - Documentation, months ago. Viewed 3k times. -1. 0. 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. elem.removeAttribute(name) – to remove the attribute. elem.attributes is a collection of all attributes. For most situations using DOM properties is preferable. We should refer to attributes only when DOM properties do not suit us, when we need exactly attributes, for instance: We need a non-standard attribute.

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"));

View Helper - HeadScript - Manual - Documentation, HeadScript Helper — Zend Framework 2 2.0.7 documentation Finally, you can also use the headScript() method to quickly add script elements; the signature for this is headScript($mode = 'FILE', $spec, $placement = 'APPEND'). HeadScript'​s sibling helper, InlineScript, should be used when you wish to include scripts  The createAttribute() method creates an attribute with the specified name, and returns the attribute as an Attr object. Tip: Use the attribute.value property to set the value of the attribute. Tip: Use the element.setAttributeNode() method to add the newly created attribute to an element. Tip: Often, you will want to use the element.setAttribute() method instead of the createAttribute() method.

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"));

Adding Scripts to a Web Page – Using Zend Framework 3, View Helper - HeadScript — Zend Framework 2 2.4.9 documentation In the case of the * File() methods, $src is the remote location of the script to load; this is Finally, you can also use the headScript() method to quickly add script HeadScript overrides each of append(), offsetSet(), prepend(), and set() to enforce usage  If the specified attribute already exists, only the value is set/changed. Note: Although it is possible to add the style attribute with a value to an element with this method, it is recommended that you use properties of the Style object instead for inline styling, because this will not overwrite other CSS properties

View Helpers, When the function is executed, a simple alert window with the "Hello World! To link an external JS file to your HTML page, you add the <script> element like Other methods provided by HeadScript view helper (such as prependFile() Below, an example is presented which can be used to append an inline JavaScript  HeadScript overrides each of append(), offsetSet(), prepend(), and set() to enforce usage of the special methods as listed above. Internally, it stores each item as a stdClass token, which it later serializes using the itemToString() method. This allows you to perform checks on the items in the stack, and optionally modify these items by simply modifying the object returned. The HeadScript helper is a concrete implementation of the Placeholder helper.

Attributes and properties, You can also register helpers by adding them to the plugin broker, or the plugin locator the broker composes. Zend Framework comes with an initial set of helper classes. The RDFa doctype allows XHTML to validate when the '​property' meta tag Finally, you can also use the headScript() method to quickly add script  The appendChild() method appends a node as the last child of a node. Tip: If you want to create a new paragraph, with text, remember to create the text as a Text node which you append to the paragraph, then append the paragraph to the document. You can also use this method to move an element from one element to another (See "More Examples").

Everything I Know About The Script Tag, When a standard attribute changes, the corresponding property is auto-updated, and (with some  If you need to add attributes to textbox to make it disable or enable,then you should do this: Textbox1.Attributes.Add("readonly","true"); By doing the above you can disable a textbox and make it false where ever you required it.

Comments
  • It would be better if you contacted the author of the php script to find the correct syntax. You haven't supplied enough information for use to work on.
  • @jeff: I was not able to find any. I posted this question here hoping anyone who may have tried this would help me.
  • Still the same thing happens
  • Can you paste the code for $this->headScript()->appendFile()
  • You added the function invocation code, but function definition code is required as this does not seem to a standard API.
  • Could you help me with this. This my first time with php and I was not able to find a complete code with headscript usage.
  • -1 You posted the correct method signature but your examples both try to use the $type argument to include the attributes. The attributes array should be passed as the 3rd parameter.