I must be be missing something really basic with Webpack or JS. I just want to call a bundled function in a form's submit function. I get a function not found message and I can't figure out why.
Error index.html:13 Uncaught ReferenceError: double is not defined
index.html (closing tags removed for brevity)
<head>
<script type="text/javascript" src="bundle.js"></script>
<body>
<form name='myform' onsubmit="doStuff(event); return false;">
<button type="submit">Press Me!</button>
<script type="text/javascript">
function doStuff(e) {
e.preventDefault();
console.log(double(32));
}
abc.js (webpack entry point)
var a = require('./double');
double.js
function double(number) {
return number * 2;
}
module.exports.double = double;
Bundle...
$ webpack abc.js bundle.js
Hash: 0e65404017702c54dac5
Version: webpack 1.13.2
Time: 59ms
Asset Size Chunks Chunk Names
bundle.js 1.58 kB 0 [emitted] main
[0] ./abc.js 28 bytes {0} [built]
[1] ./double.js 80 bytes {0} [built]
Any help appreciated
The problem is trying to mix page and bundled JS. They're in different scope, hence the bundled method not being visible.
I moved the onsubmit function from the page to the entry file, running after window.load. The window object is of course global so the bundled code can add event handlers to any DOM element.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.