Stuck in onclick property

Subscribe to Stuck in onclick property 4 posts, 3 voices

 
Piotr P. Kar... Scriptwright

I want to simulate the click on an Anchor tag. I retrieve the DOM element and can read the contents of the onclick property (it's something like "(new Image()).src = '/ajax/sdfafaf'; return"), but I haven't found the way to execute it.

 
znerp Scriptwright

I've not tried it, but wouldn't something like the following work...

newScript = document.createElement("script");
newScript.type = "application/x-javascript";
newScript.innerHTML = "(new Image()).src = '/ajax/sdfafaf'; return)";
document.getElementsByTagName("head")[0].appendChild(newScript)

 
Mindeye Scriptwright

It's easier to create a fake event:

var myNode = document.getElementById("mynodeid");
var clickEvt = document.createEvent("MouseEvent");
clickEvt.initEvent("click", true, true);
myNode.dispatchEvent(clickEvt);

 
Piotr P. Kar... Scriptwright

Thanks Mindeye!

It worked. Actually it was the mousedown event that triggered the action.