-1

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.

jiyi
  • 101
  • 9
  • 1
    are you sure `hello.js' %}` is correct ? or should be like this `{% 'hello.js' %}` ? – Swati May 05 '21 at 09:12
  • Sorry, bad copy/paste. Question updated. – jiyi May 05 '21 at 09:13
  • Does this answer your question? [Accessing controls early | load vs. domready](https://stackoverflow.com/questions/8934993/accessing-controls-early-load-vs-domready) – BGPHiJACK May 05 '21 at 09:25
  • 1
    Theoretically it could also be possible that your inline code runs before hello.js is loaded. – Alex May 05 '21 at 09:37
  • @blanknamefornow baby wakes up early. Check your solution later. Thx in advance. – jiyi May 05 '21 at 10:18

2 Answers2

0

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

   <script src="~/hello.js"></script>
   $(document).ready(function(){
     hello();
   });
Suleman
  • 1
  • 2
0

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

jiyi
  • 101
  • 9