0

I know this question has been asked a lot, but none of the solutions to the question have worked for me. This is for a Google Chrome Extension. I am calling a javascript function in an html file like this:

    <div style="text-align:center">
        <input type="button" onClick="buttonClicked()" value="Activate Extension">
    </div>

    <script type="text\javascript" src="eventPage.js"></script>

    </body>

</html>

in eventPage.js:

function buttonClicked() {
  var uname = document.getElementById("uname").value;
  alert("Extension activated. Enjoy!");
}

I have tried moving the html tag in the head and the body and nothing changes. Any and all help is greatly appreciated.

evanhaus
  • 727
  • 3
  • 12
  • 30
  • Your `type` is the wrong value: `type="text\javascript"`, it wont execute the code inside the file if its the wrong type. text/javascript – Patrick Evans Jun 29 '17 at 19:00
  • First of all the script type should be `type/javascript` or better removed entirely. – XCS Jun 29 '17 at 19:01
  • @ Patrick Evans This is good, thank you. Still won't popup the alert , however. – evanhaus Jun 29 '17 at 19:04
  • Check your console for errors – Patrick Evans Jun 29 '17 at 19:05
  • See if you can call your `buttonClicked` function from the browser JS console. You can also try placing the function inside a `script` block for diagnosis. The result of these tests might hep you go forward – James Poulose Jun 29 '17 at 19:11
  • 1
    Inline code doesn't work in extension pages. Use addEventListener in a separate js file. Also do read the corresponding console for your page/popup, you'll see the error. – wOxxOm Jun 29 '17 at 20:06
  • 1
    The name you're using for the js script implies you misunderstand the extension architecture. An event page is a hidden separate nonpersistent background page that can't be shown and thus its buttons can't be clicked by a user. – wOxxOm Jun 29 '17 at 20:09
  • @ wOxxOm so should I change something about it in my manifest file? I have it as a content script – evanhaus Jun 29 '17 at 20:37
  • Please [edit] the question to be on-topic: include a [mcve] that duplicates the problem. For Chrome extensions or Firefox WebExtensions this almost always means including your *manifest.json* and some of the background, content, and/or popup scripts/HTML. Questions seeking debugging help ("why isn't this code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it *in the question itself*. Please also see: [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Jun 30 '17 at 04:00
  • You say "none of the solutions to the question". Please sate which ones you have tried. If you don't, then we are just going to be pointing you at the ones we think are the problem, as those are the most likely. – Makyen Jun 30 '17 at 04:01
  • I suggest you read the [Chrome extension overview](https://developer.chrome.com/extensions/overview) (perhaps along with the pages linked from the overview). The [architecture section](https://developer.chrome.com/extensions/overview#arch) has overall architecture information which should help your understanding of how things are generally organized/done. – Makyen Jun 30 '17 at 04:08
  • Possible duplicate of: [The Chrome extension popup is not working, click events are not handled](https://stackoverflow.com/q/17601615), but we don't know for sure due to not having a *complete*, but minimal, [MCVE] which duplicates the issue, including a *manifest.json*. – Makyen Jun 30 '17 at 04:09

2 Answers2

0

Changing the type should work. If not check if the external reference path is correct and all the javascript is available.

Also check if you see any errors in developer console.

Try to add some breakpoints and see if that function is being called.

praveen
  • 489
  • 6
  • 12
  • While this is part of the problem with the code in the question, it's not going to get it solved. It should be noted that the OP had stated in a comment that correcting the `type` did not solve the problem 15 minutes prior to you posting this. – Makyen Jun 30 '17 at 04:05
-1

It's because of this line var uname = document.getElementById("uname").value; where you are giving a variable to the button value.It is only needed when there is input type "text" where there will be text in the text box.

Your code should be following and it works fine.

<div style="text-align:center">
    <input type="button" onClick="buttonClicked()" value="Activate Extension">
</div>

<script src="eventPage.js"></script>

</body>

Just remove variable line from the javascript.

 function buttonClicked() {
  alert("Extension activated. Enjoy!");
}
srikar reddy
  • 46
  • 1
  • 10