javascript - Click event fires on document load Tampermonkey -



javascript - Click event fires on document load Tampermonkey -

i'm writing tampermonkey script adds button page , adds onclick event button. have button on page want it, when seek attach click event using "addeventlistener" recommended in related questions click events in user scripts, event handler fires on page load , not when button clicks.

var testbutton = document.createelement("button"); testbutton.addeventlistener('click', alert("working"), false); testbutton.id = "testbutton"; testbutton.innerhtml = "this button"; testelement.appendchild(testbutton);

basically, when page loads "working" alert fires, not when click button. no console feedback either. missing?

that's because you're calling alert on pageload, not on click, wanted anonymous function well

var testbutton = document.createelement("button"); testbutton.addeventlistener('click', function() { alert("working"); }, false); testbutton.id = "testbutton"; testbutton.innerhtml = "this button"; testelement.appendchild(testbutton);

whenever add together parentheses function, it's called immediately, phone call function on event, want reference it, here examples

addeventlistener('click', alert("working"), false); // called addeventlistener('click', alert, false); // called on click addeventlistener('click', function() {}, false); // called on click addeventlistener('click', function() { alert(); }, false); // called on click

javascript tampermonkey

Comments

Popular posts from this blog

Using Android Volley to post an array to PHP -

python - How to add an model instance to a django queryset? -

angularjs - No 'Access-Control-Allow-Origin' error from local domain to public API -