22

First , code in resources/js/app.js

function button1Clicked(){
  console.log('Button 1 is clicked');
}

Second , code in testing.blade.php

<!DOCTYPE html>
<html>
<head>
  <meta name="csrf-token" content="{{ csrf_token() }}">
  <title>Testing</title>
</head>
<body>
  <button onclick="button1Clicked()">Button 1</button>
  <button onclick="button2Clicked()">Button 2</button>

  <script type="text/javascript" src="{!! asset('js/app.js') !!}"></script>
  <script type="text/javascript">
    function button2Clicked(){
      console.log('Button 2 is clicked');
    }
  </script>
</body>
</html>

After npm run dev and testing in localhost

enter image description here

As a result, JavaScript function button1Clicked() in resources/js/app.js is not defined. but the function button2Clicked() in testing.blade.php can be defined. Is it a bug from Laravel Mix or I did some mistakes

4
  • Could you add the complete contents of your webpack.mix.js and app.js files? Commented Oct 9, 2017 at 10:13
  • 1
    iCoders is correct, you need to use {{ }} and not {!! because {!! are used to display unescaped content Commented Oct 9, 2017 at 10:33
  • @iCoders i have tried change it to {{ but still doesn't work Commented Oct 9, 2017 at 11:27
  • @JeffreyWesterkamp I completed the content already and checked the script is in my public/js/app.js too Commented Oct 9, 2017 at 11:28

4 Answers 4

63

I had the same problem as you.

It turned out that when Laravel-mix, aka Webpack, compile the js, it wraps our function in a closure.

For example, when we define a function like this

function setlocale(code) {
     console.log(code);
}

Webpack will generate into

(function(module, exports, __webpack_require__) {

     function setlocale(code) {
         console.log(code);
     }

});

That's why we can access the variables or functions inside that closure, but we cannot from the outside as it limits our scope.

A simple solution is setting our function into window variable, which is a global variable.

window.setlocale = function (code) {
    console.log(code);
}
Sign up to request clarification or add additional context in comments.

4 Comments

omg, it works now. i do like this. window.YourFunction = function(ParamIfHaveParam){ // yourcode }
For better understanding window.yourVariable = function (code) { ... }
It's quite old topic but I would like to ask you, why does webpack put our function in closure? Does it have some advantages?
Hi MatějČerný, Webpack does that strategy to prevent the pollution of global environment.
8

update the code in resources/js/app.js as below-

button1Clicked = function(){
    console.log('Button 1 is clicked');
};

then it will be available for global scope.

2 Comments

Had to search a long time to find this solution. Thanks!
Glad to know that it helped you! :)
2

You just use this top of your js file.

window.prototype = this;

1 Comment

In what js file must one add the above code?
0

As window object is not accessible when building/packaging assets through laravel mix / webpack, so thats why onclick event handler becomes undefined for the code itself. So if the window.setlocale approach doesn't fit well with you then an alternative would be to bind using the jQuery click event e.g.

$("#button-id").on("click",function() => {
       // enter code here
})

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.