Some questions on writing front-end scripts
-
Hello everyone.
I'm writing a plugin for NodeBB. and some questions arises.-
I see many plugins have client.js, the purpose of writing this script is to put a short script which will run as soon as this component is loaded. Is it the true purpose of this file?
-
in client.js, we could see
$(document).ready(function() { // Some code }
example from
nodebb-plugin-markdown
But we could see another version which does not use this wrap instead expose those statements in global scope.
The question is, which version is better or is recommended?Thanks in advance!
-
-
@jiangcaiyang
$(document).ready
just tells jQuery to execute the code in the given function after the document HTML as finished loading. It may not be necessary if the things you're doing don't need the document to be ready. -
@PitaJ How many times will
// Some code
be executed if they are not inside$(document).ready
? I know// Some code
will be executed once inside$(document).ready
.Another question, which will be executed first? the code inside
$(document).ready
or the one outside?