Chrome扩展程序Chaing内容来自Javascript [英] Chrome Extension Chaing Content From the Javascript

查看:144
本文介绍了Chrome扩展程序Chaing内容来自Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试使用基本的Google Chrome Hello World类型的扩展程序。有人可以解释为什么下面的代码不起作用吗?

popup.js:

  document.getElementById( foob​​ar)。innerHTML =Hello Chrome扩展; 

popup.html:

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

我遵循 http://developer.chrome.com/extensions/getstarted.html

解决方案

它可以通过两种方式解决:


  1. 交换<< ; script。> < div ..>

  2. popup.js 中的代码在domready事件中:

     文档。 addEventListener('DOMContentLoaded',function(){
    // Code here ...
    });


您的代码失败,因为< div> 在执行脚本时未知。


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.

解决方案

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.

这篇关于Chrome扩展程序Chaing内容来自Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆