Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert not appearing in popup.html of chrome extension

I am still very new with chrome extensions and am just testing things out. Right now I have a popup.html that with a short form that I want to create an alert when submit is clicked. I cannot for the life of me figure out why it's not working.

<!doctype html>
<html>
<head>
<title>Test</title>
<script type="text/javascript" src="popup.js">

</script>
</head>
<body onload="alert('open')">
<form>
Username: <input type='text' id="username" name='username'></input>
Password: <input type='password' id='password' />
          <button onclick="alert('test');return false;">Login</button>
</form>
</body>
</html>

Any suggestions?

Edit: I even made an onload in the body tag to see if an alert would open there but it doesn't. In the popup.js I have an alert open on window.onload and that works however.

like image 675
Eric Smith Avatar asked Apr 08 '12 17:04

Eric Smith


2 Answers

The function stops after you return false. Put return false at the end of the statement, then your alert should work. Or you could take it out.

 <button onclick="alert('test')">Login</button>

Update After some research I found out the following...

Inline JavaScript will not be executed

Inline JavaScript, as well as dangerous string-to-JavaScript methods like eval, will not > be executed. This restriction bans both inline blocks and inline event handlers > (e.g. ).

Source here

like image 163
Larry Battle Avatar answered Nov 07 '22 13:11

Larry Battle


This could be your problem too: inline scripts doesnt work with "manifest_version":2

So, i tested, and with the following manifest, your code works!

{
  "name": "test",
  "version": "1.0",
  "description": "test",
  "manifest_version":1,
  "browser_action": {
    "default_icon": "images/padlock.png",
    "default_popup":"html/popup.html"
  },
  "permissions": [
    "tabs", 
    "http://*/*"
  ]
}

Anyway.. it would be better to handle actions in popup.js, but first, change button attributes in popup.html to the following: <button type="button">Login</button>

popup.js:

$(document).ready(function(){
    $(":button").click(function(){
        alert("test");
        //more code here...
    });
});

Remember to insert jquery.js into your <head> tag, in popup.html, just above of popup.js:

<head>
<title>Test</title>
<script type="text/javascript" src="../js/jquery.js"></script> 
<script type="text/javascript" src="../js/popup.js"></script>
</head>

I wish, it was useful. Regards Jim..

like image 44
Jim-Y Avatar answered Nov 07 '22 13:11

Jim-Y