3

I am unable to call a function from external javascript file in an HTML file. Kindly suggest me where am I making a mistake...

I have updated the js file.

File 1: A.js

    (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
function sayHello(){
    console.log("hello");
}





},{}]},{},[1]);

File 2: index.html

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
<div>
    <button onclick="sayHello()">click Me</button>
    <script type="text/javascript" src="bundle1.js"></script>
  </div>
<div>
  <button type="button">rotate me</button>
</div>
</body>
</html>

Now, Whenever I run the html file, I get an error after clicking the button that sayHello() is not found.

The command: browserify A.js -o bundle1.js

Kindly help me spotting the error!

5
  • 2
    you can remove the bad type="" in HTML5, or fix it with "text/javascript" Commented Aug 16, 2018 at 17:00
  • also, after fixing it, I suggest you to move your <script> tag or inside the <head> or to the complete bottom of <body> it makes your code more readable Commented Aug 16, 2018 at 17:04
  • A similar question was asked & answered here Commented Aug 16, 2018 at 17:10
  • 1
    if browserify wraps your code inside a function, the sayHello is no longer in the global scope, so inline attribute can't use it. You can add a click event listener with document.querySelector('button').addEventListener('click', sayHello) on the button (good), or you could make your sayHello global with window.sayHello = sayHello; (bad) Commented Aug 16, 2018 at 20:58
  • @progysm Hello, I followed your suggestion, but whenever I try to run using ng serve on node.js, the browserified code is not loaded. Secondly, if I run html file directly on the browser without node or other application, then no error is thrown but the nothing is printed on the console. Also, if i do this: document.querySelector('button').addEventListener('click', function(){console.log("hello"}), then hello is printed on the console. So what am I missing? Commented Aug 17, 2018 at 17:14

1 Answer 1

2

Change the script type to "text/javascript" or simply omit it as it is no longer needed in HTML5.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
<div>
    <button onclick="sayHello()">click Me</button>
    <script type="text/javascript" src="A.js"></script>
  </div>
<div>
  <button type="button">rotate me</button>
</div>
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

i am embarrassed that i missed this change!! thanks for the help
I have made a change to the code. And now again i am getting the same error. It would be great if any suggestion can be provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.