Chrome扩展程序的加载行为 [英] Chrome extension onload behaviour

查看:70
本文介绍了Chrome扩展程序的加载行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发chrome扩展程序的设置页面.在我的options.js文件中,我想使用一些默认值来初始化设置,为此使用window.onload = initSettings();.在我的initSettings()函数中,我试图通过document.getElementById("someId")从DOM访问输入.但是此调用始终返回null.我认为window.onload事件在所有DOM元素都准备就绪后触发.

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.

我在做什么错了?

推荐答案

我在options.js文件的顶部-自上次使用扩展功能以来已经存在了很长时间,我不确定帮助.值得一试.

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);

这篇关于Chrome扩展程序的加载行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆