How to get url for static image file in Magento2 javascript files?
you can make your function as a jQuery UI widget and then call it from the template and sending the image url as a parameter.
For an idea on how to create a UI widget you can check how the configurable products widget is built and how it is called in the frontend with parameters
I know there are better examples but this is the first one I found that has parameters set on a template.
See my comments in the code to what means what.
<script type="text/x-magento-init">
{
"#product_addtocart_form": { //element to map the ui widget on, you can use 'body' if it's not element specific
"configurable": { //widget name to initialize
"spConfig": <?php /* @escapeNotVerified */ echo $block->getJsonConfig() ?>, //parameter
"onlyMainImg": <?php /* @escapeNotVerified */ echo $block->getVar('change_only_base_image', 'Magento_ConfigurableProduct') ?: 'false'; ?> //parameter
}
}
}
</script>
The "Magento 2" way to do this would be
Initialize your script as a RequireJS module using the
x-magento-init
<script/>
tag, using the*
feature so its not bound to a DOM element as a Magento jQueryUI style widgetUse PHP to dynamically generate the JSON object you'll pass into your script