1 Replies - 383 Views - Last Post: 01 September 2016 - 09:01 AM

#1 shortbus   User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 235
  • Joined: 26-March 13

chrome extension not loading local JS script

Posted 01 September 2016 - 08:11 AM

im trying to build a basic chrome extension but i am having trouble using java script within my HTML popup page. when i assigned an action listener to a html button but when clicked it does not execute the script. I dont know if this is an issue with programming for chrome or what but here is the code im working with.

popup.html

<!doctype html>
<html>
  <head >
    <h1 id = "text_display">...</h1>
    <script src = "popup.js"> </script>
    <style>
      body {
        background-color: powderblue;
        width: 400px;
        height: 400px;
      }
    </style>
  </head>
  <body>
    <button id = "walk"> walk </button>
    <button id = "attack"> attack </button>
    <button id = "pick_up"> pick up </button>
    <button id = "bag"> bag </button>
  </body>
</html>



popup.js
function main() {
  // Initialization work goes here.
  alert("hi");
}

// Add event listeners once the DOM has fully loaded by listening for the
// `DOMContentLoaded` event on the document, and adding your listeners to
// specific elements when it triggers.

document.getElementById("walk").addEventListener("onclick", function(){
    //document.getElementById("demo").innerHTML = "Hello World";
    main();
});


manifest.JSON
{
  "manifest_version": 2,
  "name": "Text_RPG",
  "description": "crappy text based RPG game conviently playable from a drop down tab",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"

  },

  "background": {
    "scripts": ["background.js"],
    "persistent": false
  }

}


any help would be appreciated thanks

Is This A Good Question/Topic? 0
  • +

Replies To: chrome extension not loading local JS script

#2 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6630
  • View blog
  • Posts: 27,107
  • Joined: 12-December 12

Re: chrome extension not loading local JS script

Posted 01 September 2016 - 09:01 AM

You are loading your script before the element with id "walk" is available. Move the script to the bottom of the page, before the closing body tag.

There may be an error in your browser's console. You need to be able to discover these errors. There are links for a few browsers in my signature below.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1