Some questions on writing front-end scripts


  • Anime Lovers

    Hello everyone.
    I'm writing a plugin for NodeBB. and some questions arises.

    1. 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?

    2. 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!✈


  • Global Moderator

    @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.


  • Anime Lovers

    @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?


Log in to reply
 


Looks like your connection to NodeBB was lost, please wait while we try to reconnect.