2012-12-26 131 views
1

我在manifest.json檔案寫了下面的代碼被解僱:DOMContentReady不是在Chrome擴展

{ 
    "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沒有運行,因爲每當我點擊登錄按鈕不會提示「它正在工作」的消息。我在這裏錯過了什麼?

回答

2

據我所知,沒有DOMContentReady事件,有DOMContentLoaded

嘗試用這種替代你的代碼 -

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