Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:56:55+00:00 2026-06-09T14:56:55+00:00

i got a simple question i have these3 files popup.html : <script> function buttonClicked(button)

  • 0

i got a simple question
i have these3 files
popup.html :

<script>
    function buttonClicked(button) 
    {
        chrome.extension.sendRequest({command:button.id});
    }
</script>
    <input style="width:100%" type="button" value="Click me" id="click" onclick="buttonClicked(this)"/><br/>

background.html :

<script>
    function processRequest(request, sender, sendResponse) 
    {
        alert('hi');
        sendResponse({});
    }
    chrome.extension.onRequest.addListener(processRequest);
</script>

and contentscript.js

s = document.getElementsByClassName('st');
if (s[0].innerText != '') { 
st = new Array(); 
for (i = 0;i<s.length;i++) {
    st[i] = s[i].innerText;
}
chrome.extension.sendMessage({"message" : st}, function(response) {});
}

i would like to fire up the contentscript each time i click on the button in the popup page be cause somehow the script in the contentscript dosnt work neither or background.html nor on popup.html?

thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:56:56+00:00Added an answer on June 9, 2026 at 2:56 pm

    I’ve attempted to explain the solution and why to use it in the comments, three times. Since you don’t understand it, I’ll explain it again with references to your code:

    Popup

    • Move the inline script to an external file, say popup.js.
    • Delete the inline event listener, and also move it to popup.js.

    popup.js

    function buttonClicked(button) {
        chrome.extension.sendRequest({command: button.id});
    }
    document.getElementById('click').addEventListener('click', function() {
        buttonClicked(this);
    });
    

    popup.html

    <script src="popup.js"></script>
    <input style="width:100%" type="button" value="Click me" id="click"><br>
    

    If you test the previous code, you’ll notice that it does not work. That’s because the contents of popup.js is executed before the button (which occurs after the <script src> is created. This can be solved in two ways (use either method, but not both):

    • Recommended: Place <script src="popup.js"></script> before the closing </body> tag in popup.html. Then, you’re certain that all elements do exist when the script is executed.
    • Alternative: Wrap the event listener call in a DOMContentLoaded event:

      document.addEventListener('DOMContentLoaded', function() {
          document.getElementByUd('click').addEventListener( ... );
      });
      

    Background page.

    For a more thorough explanation on the available options, see the middle of this answer.

    • Modify the manifest file, use "background": {"scripts": ["background.js"]}.
    • Rename background.html to background.js, and remove <script> and </script> from the file.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

got a simple web site, in html. I do a simple function to move
I have a simple html dom or other scraping question. It could even work
I have got a question on regular expression. Though simple, I got some contracting
I've got a simple question but it has got me confounded. The code below
got a very simple question which I can't seem to find any answer for.
I've got a very simple question around Rails MVC/application design with a focus on
I've got a relatively simple question about asp.net MVC models. I've got a model
Simple question but I got a headache to solve this game. Example regex. [a-zA-Z0-9\s]
Simple question: If I've got a generic Error.aspx page in my Shared folder (and
Pretty simple question. I've got some Polygons and GroundOverlays defined in KML. Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.