userscript: ¿Perdí algo de JS?

I do not understand what's going on...
I have a simple userscript, that add couple DIVs, css styles and JS functions in the pages I visit
In particular, I have one DIV that trigger a JS function with a onClick listener - this function is a "toggle" function (display/hide an other DIV):

function togglegm(etat) {
 if (etat = 'on') {
    document.getElementById('greasemky').style.display = 'block';
    document.getElementById('greasemkytoggle').innerHTML = '<a href="javascript:return(false);" onClick="togglegm(\'off\');"></a>';
 } else if (etat = 'off') {
  document.getElementById('greasemky').style.display = 'none';
  document.getElementById('greasemkytoggle').innerHTML = '<a href="javascript:return(false);" onClick="togglegm(\'on\');"></a>';
var script2 = d.createElement('script');
(d.body || d.head || d.documentElement).appendChild(script2);

The DIV "greasemkytoggle" only contains a link with a onClick that trigger "togglegm('on'), and my objective is that when togglegm(on) is executed, the innerHTML of this DIV becomes a trigger for togglegm(off).
Now the weird part... when I click on my DIV greasemkytoggle, the function togglegm(on) is perfectly executed (greasemky is displayed), and the innerHTML is perfectly changed with a link for "togglegm(off)", BUT if I click again, then nothing happens.

I looked at the source code, and discovered that my JS function just disappeared (that's why nothing happened on the last click)! Now, there is an empty function replacing my togglegm():


Do you understand that kind of behaviour...?
I found nothing online for that kind of situation...

preguntado el 24 de agosto de 12 a las 21:08

Those last three lines - what are they supposed to do? -

Thank you all, I find a solution! -

2 Respuestas

GreaseMonkey runs under a much more security conscience set of rules.

Attach the event listeners using the proper DOM3 (addEventListener) method.

It is never a good idea (in user scripts or general scripting) to assign Javascript through innerHTML.

It is never a good idea to use the "javascript:" pseudo-protocol.

Respondido 24 ago 12, 21:08

Thank Jeremy, you gave my an idea... I will try an other solution - jrm

Los problemas son etat = 'on' y etat = 'off'.

If you want to set values, use

etat = 'on'
etat = 'off'

If you want to compare, use:

etat == 'on'
etat == 'off'

Por otro lado, href="javascript:return(false);" throws an error on Firefox because there is a return outside a function (SyntaxError: return not in function). You should do href="javascript:void(0);"o return false at the end of the onclick event.

Anyway, I don't understand very well what you are doing here:

var script2 = d.createElement('script');
(d.body || d.head || d.documentElement).appendChild(script2);

Tienes una función togglegm loaded to browser's memory by a <script> elemento.

Then, you create a new <script> with that function and append it to the document, in order to load it to browser's memory again (I guess).

¿Por qué?

Respondido 24 ago 12, 21:08

in fact I write a function, and with the appendChild function I inject the function in the webpage - jrm

@jrm But why do you want to inject the function to the page if you have declared it before? - Oriol

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.