admin管理员组文章数量:1189388
I can get browserify working but am a little confused about how to access the functions in bundle.js from the DOM.
I have three files-
message.js
module.exports = function (){
return "Hello";
};
main.js
var getMessage = require('./message');
//This shows the alert when script loads
alert(getMessage());
//cannot find this function from index.html
function fnClick(){
alert(getMessage());
}
index.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="js/bundle.js"></script>
</head>
<body>
<button onclick="fnClick();">Click</button>
</body>
</html>
In the browser when the script loads alert(getMessage());
in main.js shows the alert but debugger, fnClick is undefined and I can work the scope.
Thanks
I can get browserify working but am a little confused about how to access the functions in bundle.js from the DOM.
I have three files-
message.js
module.exports = function (){
return "Hello";
};
main.js
var getMessage = require('./message');
//This shows the alert when script loads
alert(getMessage());
//cannot find this function from index.html
function fnClick(){
alert(getMessage());
}
index.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="js/bundle.js"></script>
</head>
<body>
<button onclick="fnClick();">Click</button>
</body>
</html>
In the browser when the script loads alert(getMessage());
in main.js shows the alert but debugger, fnClick is undefined and I can work the scope.
Thanks
Share Improve this question asked Aug 27, 2014 at 0:35 Dave PileDave Pile 5,7644 gold badges37 silver badges51 bronze badges 3 |3 Answers
Reset to default 24Any code in entry file is executed in the closure. If you look at the created bundle.js, you will see something like this in there.
function(require, module, exports) {
function fnClick() {
alert(getMessage());
}
}
Anything you create in here will be just hidden to global space unless you explicitly use window
object (but don't do that) to expose it.
As @elclanrs said in comments above, just attach the event listener in your main.js code instead of HTML. If you don't want to use external libraries, you can do it hard way.
var button = document.getElementById('my-button'); // add id="my-button" into html
button.addEventListener('click', fnClick);
Or with library like popular jQuery you would just call.
$('#my-button').click(fnClick)
Perhaps, you would like to consider domready package? See browserify and document ready? for a great example.
If you are usind domready, your example will become like this:
message.js (stays the same)
module.exports = function (){
return "Hello";
};
main.js (take note of the changes)
var domready = require("domready");
var getMessage = require('./message');
domready(function () {
//This shows the alert when script loads
alert(getMessage());
function fnClick(){
alert(getMessage());
}
});
index.html (stays the same)
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="js/bundle.js"></script>
</head>
<body>
<button onclick="fnClick();">Click</button>
</body>
</html>
1) install dom-event-listener as for: https://www.npmjs.com/package/dom-event-listener
2) in main.js add something like:
var domEventListener = require('dom-event-listener');
var element = document.getElementById('my-button');
domEventListener.add(element, 'click', function(event) {
console.log(event);
});
本文标签: javascriptBrowserifyhow to access mainjs functionsStack Overflow
版权声明:本文标题:javascript - Browserify, how to access main.js functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738411115a2085328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
bundle.js
file. – elclanrs Commented Aug 27, 2014 at 0:38