0

I need to add some on my JS script on footer instead of header since I want to use some things after DOM loaded. I've already used the core function addJs('script url') there is no parameter to choose where to include JS scripts.

Taha Azzabi
  • 2,392
  • 22
  • 31
  • please mention that you need to add this js in back-end side for plugins , other wise user thinks that you need to add it in front-end. – Hardik Satasiya Jan 15 '18 at 12:52

1 Answers1

1

October Cms Backend shipped with jQuery v2.1.3already, so you can utilize document ready event for your java-script code.

back-end templates location is modules\backend\layouts and _head is having <?= $this->makeAssets() ?> so all assets will be inserted here and its not good to change here.

just use

$this->addJs('/plugins/acme/blog/assets/javascript/some-code.js');

now inside your js file (some-code.js) wrap all of your code inside document readymethod

$(document).ready(function(event) {
  // this code will execute only after DOM is loaded.
  console.log('dom is loaded !')

  // your code..


});

now, its not matter where your code is header or footer, your code is always execute after DOM is loaded.

if you have any further problem then this one, just share your problem with proper details we can help you to solve it

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • i plane use Vuejs witch i need to call script at the end of page,because i have this warning Cannot find element: #myApp – Taha Azzabi Jan 15 '18 at 13:10
  • hmm, where are you adding this `#myApp` and how you add this can you show us – Hardik Satasiya Jan 15 '18 at 13:13
  • i have a controller named "Position" with an empty function index(),i've created a file view named position/index.html ; here's the content of file `
    Hello world
    `
    i've had a file custom.js wicth i use to instance a VueJs Object here it is the content new `Vue({ el: '#myApp' })`
    – Taha Azzabi Jan 15 '18 at 13:18
  • i manage to fix this error by adding `window.onload = function () { new Vue({ el: '#myApp' }) }`, it's not possible to add script JS in footer backend plugin ,thought. – Taha Azzabi Jan 15 '18 at 13:27
  • ok cool, `$(document).ready(function(event) {})` is just wrapper of `window.onload`, so will work as same as you did, nice work ! – Hardik Satasiya Jan 15 '18 at 13:29