force a real click on an img?

littlemc

Junior Member
Joined
Sep 5, 2011
Messages
191
Reaction score
146
hey guys
i am trying to force a click on an image which will later do some other function
now i tried to do it with click() BUT it won't work for me as it does not counts as user gesture and the function can only run if the trigger was a user gesture
i need the function to get running on a page visit, i can not edit the function itself
please help me
thanks
 
If the image is inside another frame, you can't programmatically issue a click event. If it 's on the same frame, here you go:

Code:
function clickElement(elementid)
 {
var target=document.getElementById(elementid);
if(document.dispatchEvent) { // W3C
    var oEvent = document.createEvent( "MouseEvents" );
    oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target);
    target.dispatchEvent( oEvent );
    }
else if(document.fireEvent) { // IE
    target.fireEvent("onclick");
    }    
}
 
won't work for me :S
 
this would be my code after the code you gave me:

<img src="someimageURL" id="dlImage"/>
<script>
function clickElement(elementid)
{
var target=document.getElementById("#dlImage");
if(document.dispatchEvent) { // W3C
var oEvent = document.createEvent( "MouseEvents" );
oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target);
target.dispatchEvent( oEvent );
}
else if(document.fireEvent) { // IE
target.fireEvent("onclick");
}
}
$(document).ready(function(){
$("#dlImage").click(function() {
here goes the function that needs user gesture
});
});

</script>

edit:
sorry for double post
 
Why don't you just use an invisible iframe and have it follow the users mouse? Just get them to click anywhere on the page.
 
Why don't you just use an invisible iframe and have it follow the users mouse? Just get them to click anywhere on the page.

than i will just lose alot of users if no one will actually click, i am planning to just send traffic over the page or iframing the page to facebook fan page after i will make it really working
 
Put
Code:
clickElement('dlImage');

after this

Code:
$("#dlImage").click(function() { 
 here goes the function that needs user gesture
 });
 
thanks for the help
but
after adding this what i get is :cant call method 'dispatchonevent' of null
 
Last edited:
Google form auto submission. Its where javascript loads the form on page load.
 
Back
Top