Element Insertion question

A

Alex

I am trying to insert Javascript code with grease monkey so it would
insert to a page a call to a parent frame function al()

I tried (portion of the code):

x.document.createElement('script');
x.setAttribute("language", "javascript");
x.setAttribute("type", "text/javascript");
x.innerHTML ="window.load = parent.al();";
document.getElementsByTagName('head')[0].appendChild(x);

And (another try):

document.getElementsByTagName('body')[0].setAttribute("onload",
"javascript:parent.al();");


None of them seems to do anything. Is there a Javascript error in my
reasoning or some other evident problem with the code?
 
L

Lasse Reichstein Nielsen

Alex said:
I am trying to insert Javascript code with grease monkey so it would
insert to a page a call to a parent frame function al()

I tried (portion of the code):

x.document.createElement('script');

What is "x". I assume it's a reference to the current window.
x.setAttribute("language", "javascript");

If wo, you are calling "setAttribute" on the window.
x.setAttribute("type", "text/javascript");
x.innerHTML ="window.load = parent.al();";

And setting innerHTML on the window (bad!).
I wouldn't use innerHTML to set the content of a script, since the
content type of a script element is CDATA, not html. It might work
anyway, though.

The script you try to run is
window.load = parent.al()
This
1) calls the parent.al function immediately, not on load
2) assigns the result of this call to window.load, which isn't
a special property. You might be thinking of window.onload.
document.getElementsByTagName('head')[0].appendChild(x);

Not knowing the constraints of greaseMonkey, I would try this:

var s = document.createElement("script");
s.type="text/javascript";
s.text = "window.onload = parent.al";
document.body.appendChild(s)

(add "x." before "document" if greaseMonkey requires it).

However, it would probably be better to just do:

window.onload = parent.al

directly as the greaseMonkey script, instead of trying to build a new
script element and have it executed. You are after all already executing
a script.
And (another try):

document.getElementsByTagName('body')[0].setAttribute("onload",
"javascript:parent.al();");

The body element can be found as "document.body", which is just as
standard compliant as using getElementsByTagName (and even the same
standard).

Setting onload properties using setAttribute is not as safe as setting
them directly. Just do:
document.body.onload = function(){ parent.al(); }
or
document.body.onload = parent.al;

However, you might want to avoid setting onload from a greaseMonkey
script this way, since it might overwrite what the page put there.
Use a non-destructive way instead:

document.body.addEventListener("load", parent.al, true);

None of them seems to do anything. Is there a Javascript error in my
reasoning or some other evident problem with the code?

What browser are you using? (Both Firefox and Opera supports
greaseMonkey scripts, but I'll assume it's Firefox).
What does the Javascript console print?

When does greaseMonkey execute its scripts? Has the page already loaded?
(then assigning to onload properties won't do anything anyway)?

/L
 
M

Martin Honnen

Alex said:
I am trying to insert Javascript code with grease monkey so it would
insert to a page a call to a parent frame function al()

I tried (portion of the code):

x.document.createElement('script');
x.setAttribute("language", "javascript");
x.setAttribute("type", "text/javascript");
x.innerHTML ="window.load = parent.al();";
document.getElementsByTagName('head')[0].appendChild(x);

Simply doing
window.addEventListener(
'load',
function (evt) {
if (typeof parent.al != 'undefined') {
parent.al();
}
},
false
);
in your Greasemonkey script suffice to add an event listener for the
load event which then calls the function named al in the parent if it is
available.
 
A

Alex

For Martin Honnen:
---------------------------------------------------
Error: [Exception... "'Permission denied to get property Window.al'
when calling method: [nsIDOMEventListener::handleEvent]" nsresult:
"0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" location: "<unknown>"
data: no]
---------------------------------------------------

For Lasse Reichstein Nielsen:
(first, allow me to thank you for your step by step advice, it should
have been "x=" not "x.")
Your way also gives an error:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top