LoginSignup
12
6

More than 5 years have passed since last update.

webpackでビルドしたbundle.jsの関数をonclickで動かしたかった話。

Posted at

久々にjsを直接書く機会が偶々あって、「webpackとかなにこれめっちゃ便利じゃん」とか思って使ってみたらindex.jsに定義してるはずの関数をonclickで呼ぼうとするとnot definedになったので解決方法の記録

【やりたかったこと】

index.html

<input type="button" value="送信" onclick="hoge()"/>

これをクリックしたときに↓を動かしたかった

index.js

function hoge() {
    console.log("ウオアアアアアアア");
};

とりあえず試したらnot definedとか言われたので、bundle.js開いてみたらそもそも定義した関数が存在してないっていうw

【解決法】

index.js

var hoge = function hoge() {
    console.log("ウオアアアアアアア");
};

//↓こいつが必要みたい
window.hoge = hoge;

↓でもできるんじゃね?って思ったけど、多分またしばらくjs書かないので試しませんでした。

index.js

window.hoge = function a() {
    console.log("ウオアアアアアアア");
};
12
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
6