简体   繁体   中英

Do I need to 'document.ready()' this piece of Javascript?

I use freemarker to generate HTML pages on the fly for user requests. These include a link to a javascript file in the header. In this file, an array is defined. I need to assign the value of myArray[0] for each generated page with a different and dynamic value.

I was thinking about about adding a script in the HTML:

<body>
  <script>myArray[0]="MyDynamicValue";</script>
  ...
</body>

I am wondering whether I should execute this initialization in (for example) JQuery's document-ready? I don't think so, but I am hesitating...

Nothing about it depends on accessing the DOM, so there is no need to wait for the DOM to be ready.

(Nothing there constructs a myArray object though, so in the absence of anything other code, this will throw an error… but waiting for the DOM to be ready won't change that unless it coincidently also causes it to be executed after myArray is created.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM