1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

force a real click on an img?

Discussion in 'HTML & JavaScript' started by littlemc, Feb 22, 2013.

  1. littlemc

    littlemc Junior Member

    Joined:
    Sep 5, 2011
    Messages:
    191
    Likes Received:
    142
    Occupation:
    Making Money
    Location:
    Not There
    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
     
  2. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    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");
        }    
    }
    
     
  3. littlemc

    littlemc Junior Member

    Joined:
    Sep 5, 2011
    Messages:
    191
    Likes Received:
    142
    Occupation:
    Making Money
    Location:
    Not There
    won't work for me :S
     
  4. littlemc

    littlemc Junior Member

    Joined:
    Sep 5, 2011
    Messages:
    191
    Likes Received:
    142
    Occupation:
    Making Money
    Location:
    Not There
    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
     
  5. 0_00_0

    0_00_0 Senior Member

    Joined:
    Oct 7, 2010
    Messages:
    1,024
    Likes Received:
    486
    Location:
    Canada
    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.
     
  6. littlemc

    littlemc Junior Member

    Joined:
    Sep 5, 2011
    Messages:
    191
    Likes Received:
    142
    Occupation:
    Making Money
    Location:
    Not There
    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
     
  7. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    Put
    Code:
    clickElement('dlImage');
    
    after this

    Code:
    $("#dlImage").click(function() { 
     here goes the function that needs user gesture
     });
    
     
  8. littlemc

    littlemc Junior Member

    Joined:
    Sep 5, 2011
    Messages:
    191
    Likes Received:
    142
    Occupation:
    Making Money
    Location:
    Not There
    thanks for the help
    but
    after adding this what i get is :cant call method 'dispatchonevent' of null
     
    Last edited: Feb 23, 2013
  9. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
  10. Optimism

    Optimism Newbie

    Joined:
    Apr 20, 2013
    Messages:
    23
    Likes Received:
    2
    Google form auto submission. Its where javascript loads the form on page load.