0

my alertHi.js will be loaded but i cant use it from the Theme.

JS-Source alertHi.js:

this ( inc/Components/Darkmode/src/alertHi.js ) will be executed when i load the page:

function alertHi(){ alert('hi') }
alertHi();
console.log('is loaded');

is defined when loading

But

html-Source

<a onclick="alertHi()">☀️</a>

creates this error:

Error: alertHi is not defined

but if i call the function alertHi() from the page i get Error:

Uncaught ReferenceError: alertHi is not defined
    onclick http://localhost/wordpress/:1

Any idea?

JS-Min-Source dist/alertHi.min.js:

webback has generated dist/alertHi.min.js

/******/ (() => { // webpackBootstrap
/******/    // The require scope
/******/    var __webpack_require__ = {};
/******/    
/************************************************************************/
/******/    /* webpack/runtime/make namespace object */
/******/    (() => {
/******/        // define __esModule on exports
/******/        __webpack_require__.r = (exports) => {
/******/            if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/                Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/            }
/******/            Object.defineProperty(exports, '__esModule', { value: true });
/******/        };
/******/    })();
/******/    
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other entry modules.
(() => {
/*!*************************************************!*\
  !*** ./inc/Components/Darkmode/src/alertHi.js ***!
  \*************************************************/
function alertHi() {
  alert('alertHi says hello :)');
}
alertHi();
console.log('script loaded');
})();

// This entry need to be wrapped in an IIFE because it need to be in strict mode.
(() => {
"use strict";
/*!***************************************************!*\
  !*** ./inc/Components/Darkmode/src/alertHi.scss ***!
  \***************************************************/
__webpack_require__.r(__webpack_exports__);
// extracted by mini-css-extract-plugin

})();

/******/ })()
;
//# sourceMappingURL=alertHi.min.js.map

was reading before:

and tried a tip read here Cannot call javascript function in wordpress

but get the error jQuery is not defined

will also not be found if i using

(function($) {
    function alertHi(){
       alert('hi')
    }
})(jQuery);
SL5net
  • 2,282
  • 4
  • 28
  • 44
  • 1
    _"webback has generated dist/darkmode.min.js"_ - and where does that actually get embedded now - in the head, or the footer? – CBroe Sep 22 '22 at 10:30
  • `☀️` - why would something this "ugly" be in your theme file to begin with; why is the click handler not added _via_ JavaScript / jQuery? – CBroe Sep 22 '22 at 10:32
  • @CBroe its not embedded in the head and not in the footer. is it not embedded then? maybe this is the reason. wondering that i get the alert message when i load the page but this error (not defined) if i press the link. – SL5net Sep 22 '22 at 10:38
  • 1
    Well if it wasn't included at all, you could hardly get the alert. Maybe it gets combined into one single JS resource, together with the code from other files ...? – CBroe Sep 22 '22 at 10:39
  • @CBroe i don't know why the click i added like so. is it wrong? it works if i put the js-Souce into the page `single.php` – SL5net Sep 22 '22 at 10:41
  • Well it is very "old-school", to add event handlers via HTML attributes. This should rather be done using `addEventListener` in native JavaScript, or using `.on()` in jQuery. – CBroe Sep 22 '22 at 10:42
  • yes its combined i think. by webpack – SL5net Sep 22 '22 at 10:42
  • @CBroe i get same error (`ReferenceError: is not defined `) if i use `addEventListener` – SL5net Sep 22 '22 at 11:09

2 Answers2

2

webpack by default wraps all of the bundle internally, you can't access it externally unless explicitly setting it in the webpack config as a library. you can also hack it out and add window.alertHi = alertHi in your js code but this is not recommended.

please refer to webpack's configuration API and set your bundle output to be a library. (preferably - UMD/commonjs library)

bugkiller
  • 124
  • 3
  • but it runs when page loaded. It just can't be executed later from the page. Does it make sense? – SL5net Sep 22 '22 at 11:07
0

inspired by answers below this question ( Define global variable with webpack ) and this answer here https://stackoverflow.com/a/35825562/2891692 i found a solution:

Use the global window object

window.alertHi = function (){ alert('hi') }
SL5net
  • 2,282
  • 4
  • 28
  • 44
  • this is more of a hack rather than the right solution. the right solution would be to configure webpack to output your bundle as a library and then it will already define it on your window object to use as you wish. please take a look - https://webpack.js.org/configuration/output/#outputlibrary – bugkiller Oct 12 '22 at 11:25