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 newsc开发者_如何学Gorpt;
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!
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.
精彩评论