Chrome扩展程序:onclick()事件未触发alert()弹出窗口

时间:2013-04-02 14:18:33

标签: javascript javascript-events google-chrome-extension event-handling google-chrome-app

无法使用onclick()事件触发alert()弹出

代码:

的manifest.json:

{
"name": "Project",
"version": "1.0.0",
"manifest_version": 2,
"description": "Popup when website requires Log in",
"browser_action":{
    "default_icon":"icon_19.png",
    "default_popup":"Popup.html"
    }
}

Popup.html:

<html>
<head></head>
<body>
<div class="plus" onclick="popup()"></div>
<script src="inline.js"></script>
</body>
</html>

inline.js:

function popup() {
var link = document.URL;
alert("This is the Link : ( " +link+ " )");
}

1 个答案:

答案 0 :(得分:4)

请勿在Chrome扩展程序中使用内联Javascript。

HTML:

<div class="plus"></div>
<script src="inline.js"></script>

JS:

function popup(e) {
  var link = document.URL;
  alert("This is the Link : ( " +link+ " )");
}

var plusBtn = document.querySelector('.plus');
plusBtn.addEventListener('click', popup);