Chrome Extension Chaing Content From the Javascript

删除回忆录丶 提交于 2020-01-25 17:27:30

问题


So, I am trying to do a basic Google Chrome Hello World kind of extension. Can someone explain me why the below code doesn't work? Thanks.

popup.js:

document.getElementById("foobar").innerHTML = "Hello Chrome Extensions";

popup.html:

<!doctype html>
<html>
  <head>
    <title>Hello Chrome</title>
    <script src="popup.js"></script>
    <div id="foobar"></div>
  </head>
  <body>
  </body>
</html>

I am following the "framework" of http://developer.chrome.com/extensions/getstarted.html.


回答1:


It can be solved in two ways:

  1. Swap the order of <script .. > and <div ..>.
  2. Wrap the code in popup.js in a domready event:

    document.addEventListener('DOMContentLoaded', function() {
        // Code here...
    });
    

Your code failed because the <div> was unknown at the time of executing the script.



来源:https://stackoverflow.com/questions/15325447/chrome-extension-chaing-content-from-the-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!