javascript - Using function of an already loaded script -
in html-page i'm loading js-file (main.js) functions.
html:
<head> <script src="main.js"></script> </head> <body> <header>navigation</header> <main> <div id="click_me"> </div> </main> </body>
main.js:
$( document ).ready(function() { start() }); function start() { $.getscript( 'first.js' ).done(function () { console.log("loaded"); }); } function anything() { console.log("done"); }
now i'm using $.getscript load js-file (first.js). file need use functions of first one.
first.js:
$('#click_me').on('click', function() { anything(); });
in example clicking on #click_me
should result in printing "done" in console.
your code looks correct. add .fail()
statement account errors stated jquery $.getscript official docs:
https://api.jquery.com/jquery.getscript/
(see handling errors)
check error marks in console and/or exception
var see if there wrong while loading file (mostly expecting path or syntax error).
Comments
Post a Comment