3

I am unable to trigger an alert() popup with an onclick() event.

Code

File 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"
        }
}

File Popup.html

<html>
<head></head>

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

File inline.js

function popup() {
    var link = document.URL;
    alert("This is the link: (" + link + ")");
}
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
anandh199g
  • 91
  • 2
  • 2
  • 9
  • Don't use any inline code, move it all to an external file. That includes the `onclick` as well. – BeardFist Apr 02 '13 at 14:19
  • @BeardFist Thanks a lot . and how can i get the url of the website ? Its just showing the url of the popup.html – anandh199g Apr 02 '13 at 14:34
  • You can use [`chrome.tabs.query`](http://developer.chrome.com/extensions/tabs.html#method-query) with `active:true` to get the current tab, then use `tab.url` to get the url. – BeardFist Apr 02 '13 at 14:39

1 Answers1

4

Don't use inline JavaScript code in a Google Chrome extension.

HTML:

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

JavaScript:

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

var plusBtn = document.querySelector('.plus');
plusBtn.addEventListener('click', popup);
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
HaNdTriX
  • 25,034
  • 10
  • 72
  • 80