最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome extension onload behaviour - Stack Overflow

programmeradmin6浏览0评论

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

What am I doing wrong?

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

What am I doing wrong?

Share Improve this question edited Oct 8, 2012 at 22:10 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Oct 7, 2012 at 15:43 cristian.petroacacristian.petroaca 3552 gold badges4 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

I have this in the top of my options.js file - It's been so long since I last played with extensions, I can't be sure it's of any help. Worth a shot..

// 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);
发布评论

评论列表(0)

  1. 暂无评论