Chrome扩展中JavaScript按钮点击无响应问题求助
Let's walk through the issues in your code and get that alert working properly:
1. Typo in the Button Event Attribute
The most immediate problem is a typo in your button's event handler: you wrote onlick instead of onclick. Browsers don't recognize the misspelled attribute, so your GetResult() function never gets called when the button is clicked.
Fix:
Update your popup.html button line to:
<input type="button" class="button" onclick="GetResult();" value="Verify">
(I also changed type="submit" to type="button" here—more on that next.)
2. Form Submission Default Behavior Interrupts Execution
Even if you fixed the typo, using a submit type button inside a <form> would trigger the browser's default form submission behavior. This means the page would refresh immediately after your function runs, making the alert appear and disappear in a split second (or not show up at all).
Two Ways to Fix This:
- Option 1 (Simplest): Keep the button type as
button(like the fix above). This removes the default submission behavior entirely. - Option 2 (If you need to keep the form submit): Modify your
GetResult()function to prevent the default event:
And update the button's onclick to pass the event:function GetResult(event) { event.preventDefault(); // Stop the form from submitting/refreshing var news = document.getElementById('news').value; alert(news); }<input type="submit" class="button" onclick="GetResult(event);" value="Verify">
3. Unnecessary Background Script Configuration
Your manifest.json includes popup.js in the background scripts section, but this is incorrect. popup.js is meant to run only when the extension's popup is open, not as a persistent background script. This could cause unexpected behavior or conflicts.
Fix:
Remove the entire background block from your manifest.json:
{ "manifest_version": 2, "name": "News Verifier", "description": "This extension Prints the news entered in textbox", "version": "1.0", "icons": { "128": "NV.png", "48": "NV.png", "16": "NV.png" }, "browser_action": { "default_icon": "NV.png", "default_popup": "popup.html" }, "permissions": [ "storage", "notifications", "contextMenus" ] }
Once you apply these fixes, reload your Chrome extension (go to chrome://extensions/, enable developer mode, click "Reload" on your extension), and the alert should pop up with your pasted news text when you click "Verify"!
内容的提问来源于stack exchange,提问作者Pratik Pathak




