javascript 警报未出现在 chrome 扩展的 popup.html 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10064999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alert not appearing in popup.html of chrome extension
提问by Eric Smith
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.
我对 chrome 扩展仍然很陌生,我只是在测试一下。现在我有一个 popup.html,它带有一个简短的表单,我想在单击提交时创建一个警报。我一生都无法弄清楚为什么它不起作用。
<!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.
编辑:我什至在 body 标签中做了一个 onload 以查看警报是否会在那里打开,但它不会。在 popup.js 中,我在 window.onload 上打开了一个警报,但是它有效。
回答by Larry Battle
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.
函数在您返回 false 后停止。将 return false 放在语句的末尾,然后您的警报应该起作用。或者你可以把它拿出来。
<button onclick="alert('test')">Login</button>
UpdateAfter 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. ).
内联 JavaScript 不会被执行
内联 JavaScript 以及危险的字符串到 JavaScript 方法(如 eval)将不会被执行。此限制禁止内联块和内联事件处理程序 >(例如)。
回答by Jim-Y
This could be your problem too: inline scripts doesnt work with "manifest_version":2
这也可能是您的问题:内联脚本不适用于“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 中处理操作,但首先,将 popup.html 中的按钮属性更改为以下内容: <button type="button">Login</button>
popup.js:
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:
请记住将 jquery.js 插入到您的<head>
标签中,在popup.html 中,就在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..
我希望,它很有用。问候吉姆..