Try to call function defined in external js from jquery $(document).ready

123 Views Asked by At

Aim: When html page loads successfully, jquery $(document).ready gets called, then call function defined in external js inside $(document).ready.

Note: jquery lib included

hello.js

function hello(){
  alert('hello');
}

index.html

<script src="hello.js"></script>
$(document).ready(function(){
  hello();
});

Above code doesn't trigger alert. New to js and jquery. Better solutions are welcomed. Many thanks in advance.

2

There are 2 best solutions below

0
Suleman On

if you have included hello.js in root directory then try to use this syntax:

   <script src="~/hello.js"></script>
   $(document).ready(function(){
     hello();
   });
0
jiyi On

Sorry to waste everyone's time. It was due to an javascript syntax error (missing close bracket due to bad copy/paste).