Question

http://35.178.2.150/rep/india please hit this URL and inspect it, and help me if you can.

Was it helpful?

Solution

Your file path for your js should be here Vendor_Modulename/web/js/myfile.js

var config = {
    paths: {            
         'myfile': "Vendor_Modulename/js/myfile"
      },   
    shim: {
    'myfile': {
        deps: ['jquery']
    }
  }
} 

After that you can load the js inside your views like

<srcipt>
 require(["jquery","myfile"],function($,myfile){
     $(document).ready(function(){
        //call your js here...
     })
 })
</script>

If still your js is not working you can add setTimeOut js built in function which can load js after few couple of seconds of your body loading.

setTimeout(function(){ alert("Hello"); }, 3000);

Actually the best solution for you is to load your js after your body loading. You can also use (document).ready or (window).load as well.

OTHER TIPS

Add an external JS in the following way :

Try this,

Add your custom js in the following path.

Vendor/Module/view/frontend/web/js/nameofjs.js

then you need to add requirejs-config.js to the following path.

Vendor/Module/view/frontend/requirejs-config.js

then add the following code in it

 var config = {
    paths: {            
            'your_js_name' : 'js/nameofjs', // loadcustomjs is an name and it can anything in lowercase
        },                                                                
    shim: {
    'your_js_name': {
        deps: ['jquery']
    },
  }
};

and in the phtml which loads in a page or wherever you want to load this js, add the below code in it.

require(['jquery','your_js_name'],function($) {
$(window).load(function () {
     /*alert('load from external jquery');*/
});});

Hope this helps and this is correct way to load any custom js in M2 as far as I know.

Peace :)

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top