cancel
Showing results for 
Search instead for 
Did you mean: 

Put a script in magento

Put a script in magento

Hello, im in dire need of help. I need to put a script (js) in my magento frontpage, never used magento before and i dont know how it works completely i tried many ways of opening the front page in some form of "edition mode" but none of them works

 

<script data-cfasync='false'>function loadAsisteClick(callback) { var AsisteClick = document.createElement('script'); AsisteClick.onload = callback; AsisteClick.async = true; AsisteClick.src='https://app.asisteclick.com/v3/widget.php?id=9656345-4991&async=1&floating=DL&deptid=0&layer=1'; var s0 = document.getElementsByTagName('script')[0]; s0.parentNode.insertBefore(AsisteClick, s0); } loadAsisteClick(function() {} ); </script>

 

this will be the script on question the problem is that i need it to be put just after the </body> tag.

If someone can help with it i will be very thankful and if some can explain me how i failed it will be even better

2 REPLIES 2

Re: Put a script in magento

@delahozephd939 

Create the default.xml file at 

app\code\[Vendor]\[Module]\view\frontend\layout\default.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
    <link src="Vendor_Module::js/js_fileName.js"/>
    </head>
</page>

Create js_fileName.js file at \Vendor\Module\frontend\web\js\ and add your required code there.

After that, run the flush command: php bin/magento c:f

 

Or you can follow the below solution:

Create requirejs-config.js file at app/code/Vendor/Module/view/frontend/

var config = {
    paths: {
        'js-file-alias': 'Vendor_Module/js/js_fileName'
    },
    shim: {
        'js_fileName-alias': {
            deps: ['jquery']
        }
    }
};

Create js_fileName.js file and add the required  code at app/code/Vendor/Module/view/frontend/web/js/

<script>
    requirejs(['jquery', 'js_fileName'], function($){
        // Your Code
    });
</script>
Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Put a script in magento


@mymilestonecard wrote:

Hello, im in dire need of help. I need to put a script (js) in my magento frontpage, never used magento before and i dont know how it works completely i tried many ways of opening the front page in some form of "edition mode" but none of them works

 

<script data-cfasync='false'>function loadAsisteClick(callback) { var AsisteClick = document.createElement('script'); AsisteClick.onload = callback; AsisteClick.async = true; AsisteClick.src='https://app.asisteclick.com/v3/widget.php?id=9656345-4991&async=1&floating=DL&deptid=0&layer=1'; var s0 = document.getElementsByTagName('script')[0]; s0.parentNode.insertBefore(AsisteClick, s0); } loadAsisteClick(function() {} ); </script>

 

this will be the script on question the problem is that i need it to be put just after the </body> tag.

If someone can help with it i will be very thankful and if some can explain me how i failed it will be even better


Step 1: Create a new module.
Step 2: Create a requirejs-config.js and a JavaScript module file.
Step 3: Create a layout update to add a template that will enable the JavaScript module.
Step 4: Create a template file.
Step 5: Add a module and test it.