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.

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

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>
  • Only disadvantage is the script is written after all script/css link hrefs. – Earlee Jul 29 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. – mtr.web Jul 29 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>
  • 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. – Earlee Jul 29 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.