0

How can I load external scripts async (and preferably also defer). I know I can add attributes to local scripts like this:

<action method="addJs"><script>js/script.js</script><params>async defer</params></action>

But how do I load external scripts? The method above does not work for external scripts.

3 Answers 3

1

You can't natively you must use a third party Module, you can try this

http://inchoo.net/magento/how-to-add-external-javascript-css-file-to-magento/

1
1

You can do it from layout XML, but it is basically the same as placing it directly into your block. Here's an example:

<block type="core/text" name="block_name">
   <action method="setText">
      <text>
         <![CDATA[<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion_async.js" async="async"></script>]]>
      </text>
   </action>
</block>
2
  • Only disadvantage is the script is written after all script/css link hrefs. Commented Jul 29, 2019 at 9:41
  • @Earlee - If you place it in a <reference name="head"> wrapper as @Devtype did, it should load (asyncronously) at the same time as the others in your <head> tag, but depending on where you place it in the xml, it may be added before or after others. Commented Jul 29, 2019 at 12:44
1

You can achieve this by below code (which isn't different from your code tbf). Tested and it works:

<reference name="head">
<action method="addJs"><script>https://api.xyz.com/xyz.js</script><params>async defer</params></action>
</reference>
1
  • 1
    In version 1.9.x, it will not work as it will be padded with your domain. so that will become https://yourshop.com/js/https://api.xyz.com/xyz.js and result in 404. Commented Jul 29, 2019 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.