从overlay.js firefox插件获取HEAD元素

发布于 2024-08-15 02:14:47 字数 608 浏览 5 评论 0原文

我有一个 firefox 的 lightwiehgt 插件,它需要将脚本注入到 HTML 中。 代码如下所示:

var head = document.getElementsByTagName("head")[0];
var newscrpt;       
newscrpt = document.createElement('script');
newscrpt.type = "text/javascript" ;
newscrpt.src = "http://blabla.com/...";
newscrpt = head.appendChild(newscrpt);

问题是 document.getElementsByTagName("head")[0] 返回 'undefined',并检查 document.getElementsByTagName("head").length 为 0。

它当前在浏览器 document.onLoad 事件上执行,但我也尝试从 window.setTimeout 调用它,以确保加载同步不是问题,但也会发生同样的情况。

有人有什么想法吗? 谢谢!

I have a lightwiehgt plugin to firefox which needs to inject a script into the HTML.
The code looks like this:

var head = document.getElementsByTagName("head")[0];
var newscrpt;       
newscrpt = document.createElement('script');
newscrpt.type = "text/javascript" ;
newscrpt.src = "http://blabla.com/...";
newscrpt = head.appendChild(newscrpt);

The problem is that document.getElementsByTagName("head")[0] returns 'undefined', and checking document.getElementsByTagName("head").length is 0.

It currently executes on the browser document.onLoad event but I also tried calling it from window.setTimeout to make sure it is not a problem with loading synchronization, but the same happens.

Any ideas from anyone?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失去的东西太少 2024-08-22 02:14:47

如果您使用 frameiframe 对象,则不应直接引用 document,而应执行以下操作

    var doc = frame.contentWindow.document;

:头。我正在使用 jQuery 向其中添加一些资源:

    $(doc).find('head').append(
                '<link type="text/css" rel="stylesheet" href="MyStylesheet.css" />');

希望它有帮助。

If you're using a frame or an iframe object, you should not reference the document directly but do something like:

    var doc = frame.contentWindow.document;

After that you can get the head. I'm using jQuery to add some resources to it:

    $(doc).find('head').append(
                '<link type="text/css" rel="stylesheet" href="MyStylesheet.css" />');

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文