未在Chrome扩展程序中触发DOMContentReady

时间:2012-12-26 15:50:11

标签: javascript google-chrome google-chrome-extension domready

我在manifest.json文件中编写了以下代码:

{
   "name":"SecondExtension",                     
   "version":"1.0",
   "manifest_version":2,
   "content_security_policy": "script-src 'self';object-src 'self'",
   "permissions":["tabs", "http://*/*", "https://*/*"],
   "browser_action": {
          "defaul_icon":"icon.png",
          "default_title":"Security" ,
          "default_popup":"pops.html"
   }
}

我在pops.html中编写了以下代码

<html>
<head>
<script src='popup.js'>
</script>
</head>
<body>

<p>Username : </p></br><input type="text"  id="name" height="20" width="50" />

<p>Password :</p></br> <input type="password"  id="password" height="20" width="50" />

<a href="pops2.html">login</a>
<button id="login">login</button>
<textarea id="return_name" rows="2" columns="20"></textarea>
</body>
</html>

popup.js中的代码是:

function check() {
    alert('its working');
}
document.addEventListener('DOMContentReady', function () {
    document.getElementById('login').addEventListener('click', check);
});

现在问题是JavaScript没有运行,因为每当我点击登录按钮时它都没有提示“它正在工作”的消息。我错过了什么?

1 个答案:

答案 0 :(得分:2)

AFAIK,没有 DOMContentReady 事件,有 DOMContentLoaded

尝试用此代码替换您的代码 -

 function check() {
    alert('its working');
 };
 document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('login').addEventListener('click', check);
 });