1

I got an iframe to insert googlemap on.my site. but I would need to give it the async attribute. hoping it can be done.

how can I do? the idea was to put the code in a wrapper module or a custom module, but I don't know how to add the async.

Any suggestion?

2 Answers 2

0

The best way is to use the Joomla Web Asset Manager, described here.

You define your javascript dependency in a joomla.asset.json file, and include your async attribute that way:

"attributes": {
    "async": true
  }

You should put this file and the javascript file inside the /media folder via your module installation xml file, eg:

<media destination="mod_mymodule" folder="media">
    <filename>joomla.asset.json</filename>
    <folder>js</folder>
</media>

If you're using a custom module then you'll need to tell Joomla to load that joomla.asset.json file, as it won't do it automatically. As described on that same page you can do:

$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wr = $wa->getRegistry();
$wr->addRegistryFile('relative/path/to/your/joomla.asset.json');
// eg $wr->addRegistryFile('media/mod_mymodule/joomla.asset.json');

If you're not familiar with the web asset manager then this video might help.

If you really want to use the old way of adding javascript then you can include in your module code:

use Joomla\CMS\Factory;

$document = Factory::getDocument();
$document->addScript("...demo.js", array(), array('async'=>'async'));

as described in Adding JavaScript and CSS to the page.

0

There is no async loading for iframes. If you're talking about loading accompanying Javascript asynchronously, Robbie's answer above is the proper one.

If you mean lazyloading the iframe, simply add loading="lazy", e.g.:

<iframe src="https://example.com"
    loading="lazy"
    width="600"
    height="400"></iframe>

Your Answer

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

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