[英]Javascript Button in Chrome Packaged App
我正在開發Chrome應用程序,但似乎無法使html / JS按鈕正常工作。 在我的background.js文件中,我有以下代碼:
chrome.app.window.create('window.html', {
// TODO- pick a more appropriate window size, create UI
'bounds': {
'width': 400,
'height': 500
}
}, function(appWin){
pageDocument = appWin.contentWindow.document;
pageDocument.getElementById("Listen").onclick = function(){
// do stuff
});
});
起初,我嘗試僅使用“文檔”而不從內容窗口獲取pageDocument對象,但這沒有用,因為“文檔”未定義。 但是,這個新代碼甚至都不允許我打開應用程序,因此我很難找出問題所在。 這絕對是getElementById.onclick函數中的內容,因為如果我注釋掉特定部分,則應用程序打開時不會顯示任何錯誤消息-但我不確定出什么問題。
Chrome CSP禁止嘗試在任何元素上分配onclick
,因為這被視為內聯代碼。
您應該附加一個事件偵聽器,在window.html
本身中它會更有意義。
// window.js, include in window.html
// Required to ensure #Listen exists
document.addEventListener('DOMContentLoaded', function () {
// Attach event listeners, not inline onclick attribute
document.getElementById("Listen").addEventListener("click", function() {
/* your event processing */
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.