Chrome boot behavior

I am developing a settings page for the chrome extension. In my options.js file, I want to initialize settings with some default values, and for this I use window.onload = initSettings(); . In my initSettings() function, I am trying to access the input from the DOM through document.getElementById("someId") . But this call always returns null . I thought the window.onload event is fired after all the DOM elements are in place.

What am I doing wrong?

+4
source share
2 answers

I have this at the top of my options.js file. Until the last time I played with the extensions, I can not be sure of any help. It’s worth taking a picture.

 // fires when script is first loaded // can't do onInit directly here, because the DOM hasn't been loaded for options.html yet // we just set an event listener for document.DOMContentLoaded - In that handler we can call onInit document.addEventListener('DOMContentLoaded', onInit, false); 
+7
source

I needed to run a piece of code when my chrome extension was open, I used the following code:

 document.addEventListener("DOMContentLoaded", function(){ checkCheckBoxes(); }, false); 

where "checkCheckBoxes" is the Javascript function that is called.

0
source

Source: https://habr.com/ru/post/1438272/


All Articles