Search code examples
javascriptphpjquerylaravellaravel-5.3

How can I set javascript function in global?


I want to set my function in global js. So it can called from anywhere

I have function this :

$(function () {
    function formatCurrency(input) {
        return input.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.");
    }
});

I put the function on the

C:\xampp\htdocs\mysystem\resources\assets\js\main.js

And I add this : <script src="{{URL::asset('/resources/assets/js/main.js')}}"></script> on the

C:\xampp\htdocs\mysystem\resources\views\layouts\app.blade

When executed, on the console exist error like this :

GET http://mysystem.dev/resources/assets/js/main.js 404 (Not Found)

How can I solve it?


Solution

  • The issue seems to be you're using asset() wrong.

    asset refers to a file in /public.

    You should compile/minimize your JS for production and put it somewhere in /public, i.e. /public/js/my.js