Shorn Jacob
Shorn Jacob

Reputation: 1251

Webpack . Calling Javascript functions at Window Level

I am webpacking my javascript

alert("Loaded out")

function showLoaded() {
    alert("Loadedin")
}

Trying to call the function showloaded from html

<!DOCTYPE html>
<html>
  <head>
      <script type="text/javascript" src="./bundle.js"></script>
  </head>
<body>
<script>showLoaded()</script>
</body>
</html>

Loaded out is being displayed by alert , but I can't seem to call the function showLoaded(). Am I missing something obvious here.

Upvotes: 0

Views: 1132

Answers (2)

Shorn Jacob
Shorn Jacob

Reputation: 1251

Found Answer here, How to run functions that converted by webpack? . found answer here.

I had to add

output {


    libraryTarget: "this"

  }

to my webpack config for making the function visible on global (window) object.

and used export on function

export function showLoaded() {
    alert("Loadedin5")
}

Upvotes: 1

NoobTW
NoobTW

Reputation: 2564

You can also expose your function this way:

alert("Loaded out");

function showLoaded() {
    alert("Loadedin");
}

window.showLoaded = showLoaded;

Upvotes: 2

Related Questions