I am trying example in / and try to make a function call as follows:
My html is:
<!DOCTYPE html>
<html>
<head>
<title>Test Browserify</title>
<script src="bundle.js"></script>
</head>
<body>
<button onclick="hello()">test</button>
</body>
</html>
and my javascript is:
var unique = require('uniq');
var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];
console.log(unique(data));
function hello(){
alert("here");
}
I did browserify main.js -o bundle.js, so I can use require successfully.
But when I click the button, I have the error:
"Uncaught ReferenceError: hello is not defined"
Any suggestion will be appreciated!
I am trying example in http://browserify.org/ and try to make a function call as follows:
My html is:
<!DOCTYPE html>
<html>
<head>
<title>Test Browserify</title>
<script src="bundle.js"></script>
</head>
<body>
<button onclick="hello()">test</button>
</body>
</html>
and my javascript is:
var unique = require('uniq');
var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];
console.log(unique(data));
function hello(){
alert("here");
}
I did browserify main.js -o bundle.js, so I can use require successfully.
But when I click the button, I have the error:
"Uncaught ReferenceError: hello is not defined"
Any suggestion will be appreciated!
Share Improve this question edited Dec 19, 2016 at 18:09 cchamberlain 18k7 gold badges60 silver badges72 bronze badges asked May 29, 2015 at 6:44 Yu LiuYu Liu 1711 silver badge3 bronze badges 1- I'm guessing that you are seeing your 1, 2, 3, 4, ,5, 6 getting printed at the js console? – cchamberlain Commented May 29, 2015 at 7:02
2 Answers
Reset to default 14Browserifies primary purpose is to make JavaScript modules privately scoped so it has no way to see what you are trying to do.
Try using
global.hello = function() { alert("hello");}
See defining global variable for browserify.
In general, this is bad practice and you should instead export public properties out of your module and reference them via the required module reference.
just try to use global.hello = function() { alert("hello");}
in your js file and then build it using browserify.