Programatically click <INPUT type=image ...> element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Csaba2000

    Programatically click <INPUT type=image ...> element

    I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE
    5.5+). This should work regardless of whether IE has focus. Normally you would do myDomElement.cl ick and the mouse
    doesn't matter, but in the case of an input image element, what happens is the submitted url has something like
    "?x=12&y=7" appended to it (the numbers vary per mouse position on the clicked element). If you hit the spacebar
    while that element has focus or do the .click path, then you wind up with "?x=0&y=0" appended to the submitted url,
    which is no good for me.


    One possible idea that I have not gotten to work is to create an approriately named hidden element (in our case
    <input type=hidden name=x> and <input type=hidden name=y>) and set up an onClick handler and insert the position
    values that way [document.all.x. value = myImageElement. offsetWidth / 2]. However, if I insert this element at the
    end of the form, what happens then is the submitted url's string has a string like "?x=14&x=0& y=0" appended, which is
    not good enough (for example, PHP will eat the x=14, and who's to say what other web servers do?) The situation is
    even worse if I insert the hidden element at the beginning of the form, because then the initial click handler will
    set the hidden element's value, but then quietly dies. Perhaps this method can be patched up, though.

    The other idea that I had was to use .click with .fireEvent as the code below illustrates (it can also be used to
    test the above hidden element idea). However, while I am able to modify the click event, the onSubmit event's values
    don't change. In particular, if I position the mouse above the image element before pressing the "Click me" button,
    then the mouse coordinates for the onSubmit event appear correct, but the final string comes out with 0's. Similar
    comments apply if you press the space bar while the focus is on the image element.

    Thanks,
    Csaba Gabor from New York

    <HTML>
    <HEAD>
    <TITLE>fireEven t testing</TITLE>
    <SCRIPT>
    var TmpOnClick; // a unique temporary variable
    function clickHandler() {
    // clickHandler is supposed to rewrite the x,y coordinates of the mouse click
    var myButton = document.getEle mentsByTagName( "INPUT")[0];
    var newEvent = document.create EventObject();

    newEvent.offset X = myButton.offset Width / 2; // middle of image
    newEvent.offset Y = myButton.offset Height / 2; // middle of image
    newEvent.cancel Bubble = false;
    newEvent.button = 1;
    myButton.onclic k = window.TmpOnCli ck; // Replace the original click handler
    myButton.fireEv ent ("onclick", newEvent); // Allow the original handler to take over
    event.cancelBub ble = true; // Cancel the on hold original event
    }

    function clickMeSub() {
    var myButton = document.getEle mentsByTagName( "INPUT")[0];
    window.TmpOnCli ck = myButton.onclic k; // Save any old click handler
    myButton.onclic k = clickHandler // Insert our click handler
    myButton.click( ); // Now invoke it
    }
    </SCRIPT>
    </HEAD>
    <BODY bgcolor=yellow style=margin-left:.4in>
    <FORM onsubmit="alert ('submit fired: '+event.offsetX +','+event.offs etY);return true;">
    <INPUT type=image
    onclick="alert( 'on click fired: '+event.offsetX +','+event.offs etY);return true;"
    title="Click with mouse to see both onClick and onSubmit fire."
    accesskey=s value=SubmitMe>
    &nbsp;
    <BUTTON onclick="clickM eSub();return false;"
    title="Simulate s a click event, but fails to submit mouse coordinates."
    accesskey=c><u> C</u>lick me</BUTTON>
    </FORM>
    </BODY>
    </HTML>


  • Dom Leonard

    #2
    Re: Programatically click &lt;INPUT type=image ...&gt; element

    Csaba2000 wrote:[color=blue]
    > I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE
    > 5.5+). This should work regardless of whether IE has focus. Normally you would do myDomElement.cl ick and the mouse
    > doesn't matter, but in the case of an input image element, what happens is the submitted url has something like
    > "?x=12&y=7" appended to it (the numbers vary per mouse position on the clicked element). If you hit the spacebar
    > while that element has focus or do the .click path, then you wind up with "?x=0&y=0" appended to the submitted url,
    > which is no good for me.
    >[/color]
    <snip>
    I gather program supplied x and y values need to be submitted if
    submission is by program or keyboard operation.

    One solution is to have the image input click handler discriminate
    between mouse click and keyboard event by means of an onmouseup handler.
    For mouse click, submission is allowed to proceed as normal with the
    browser appending mouse coordinates. Following a keyboard event,
    however, submission is cancelled and a timer used to call programmatic
    submission using form.submit functionality.

    Using one of your ideas, the programmatic submit function dynamically
    adds hidden form fields named "x" and "y" with appropriate values before
    calling form.submit, avoiding duplicate entries for x and y values in
    the process. Appears to check out in IE5, 6, NS6 and Mozilla.

    Submitting in the background (without focus) could prove difficult.
    Personally I leave confirm dialogs enabled for form submission and would
    definitely cancel a request generated whilst away from a browser window
    - but that's me.

    HTH
    Dom

    ======== test code =========

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Untitl ed strict.dtd</title>
    <script type="text/javascript">

    var imgClicked=fals e; // set by mouseup

    function imageSubmit()
    {
    if(imgClicked)
    { imgClicked = false; // in case of later cancellation
    return true;
    }
    window.setTimeo ut("progSubmit( )",50); // don't submit on this event
    return false;
    }
    function progSubmit()
    {
    var form = document.forms. testForm;
    alert("progSubm it()");
    var el = document.create Element("INPUT" );
    el.type="hidden ";
    el.name="x";
    el.value="5"; // whatever
    form.appendChil d(el);
    el = document.create Element("INPUT" );
    el.type="hidden ";
    el.name="y";
    el.value="8"; // idem
    form.appendChil d(el);
    form.submit();
    return true;
    }

    </script>
    </head>
    <body>
    <form name="testForm" id="testForm" action="test.ht ml">
    <input type="image" src="whatever.p ng" id=myImg
    onmouseup="imgC licked=true";
    onclick="return imageSubmit()">
    </form>
    </body>
    </html>




    Comment

    • Jim Ley

      #3
      Re: Programatically click &lt;INPUT type=image ...&gt; element

      On 04 Aug 2003 23:14:03 GMT, "Csaba2000" <news@CsabaGabo r.com> wrote:
      [color=blue]
      >I want to be able to programatically click on the center of an
      > <INPUT type=image ...> element (I only care about IE
      >5.5+). This should work regardless of whether IE has focus.[/color]

      Do you really?

      Are you sure you don't want to perform an http request to the server
      such as foo.x=100&foo.y =200 if so why not ask how to do that? it's a
      lot easier.

      What reason do you actually want to perform the bizarre request above?
      As always ask a question about your real problem...

      Jim.
      --
      comp.lang.javas cript FAQ - http://jibbering.com/faq/

      Comment

      • Csaba Gabor

        #4
        Re: Programatically click &lt;INPUT type=image ...&gt; element

        Dom,

        thank you, Thank You, THANK YOU! I am amazed and stunned,
        but your example is working for me, too. At first I thought
        it was because you are creating the x and y elements with
        javascript, but the point is that your method works because
        of the form.submit. The image element won't submit itself
        unless its being clicked. Very nifty! Great thinking!


        As I mentioned to the other respondant, it was only yesterday
        that I saw your post when I was looking for my original writeup
        on Google. Neither of the two replies to my original post show
        up in my Outlook Newsreader. Go figure.


        A comment on my adaptation of your solution. This is just
        me thinking out loud. Any presubmission handling should happen
        before I (my IE exerciser) get involved. So it means that I should
        hook myself into the final handler, and note the return value.
        Cancel it in any case. However, if we returned true, that's
        when I create the x,y elements and do the form.submit. Hmmm,
        I'll have a little bit of reading to do to figure out how
        to make sure I'm the last guy in the chain, but this looks
        workable and I don't introduce artifacts if the submission is
        cancelled. This should work. Cool. Majorly cool.

        I'd like to clarify your last remark, in case you see this reply.
        It was unclear to me whether you talking from the point of view of
        the client or the server. If your comment about the alert boxes
        is from the client's (user's) point of view: The user is expected
        to know the sequence of actions they are directing IE to do.
        In particular, they should expect the confirm/alert boxes. These
        can be detected, responded to, and dismissed (It's one of the
        directives that my IE exerciser (should - it's untested) allows.
        From the server's point of view, there should be absolutely zilch
        detectable difference. If there is, the simulation is not true.
        Hopefully, I've understood your point.


        Thanks again Dom,
        Csaba Gabor from New York

        Dom Leonard <doml.removethi s@senet.andthis .com.au> wrote in message news:<XOV_a.16$ QX5.1392@nnrp1. ozemail.com.au> ...[color=blue]
        > Csaba2000 wrote:[color=green]
        > > I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE
        > > 5.5+). This should work regardless of whether IE has focus. Normally you would do myDomElement.cl ick and the mouse
        > > doesn't matter, but in the case of an input image element, what happens is the submitted url has something like
        > > "?x=12&y=7" appended to it (the numbers vary per mouse position on the clicked element). If you hit the spacebar
        > > while that element has focus or do the .click path, then you wind up with "?x=0&y=0" appended to the submitted url,
        > > which is no good for me.
        > >[/color]
        > <snip>
        > I gather program supplied x and y values need to be submitted if
        > submission is by program or keyboard operation.
        >
        > One solution is to have the image input click handler discriminate
        > between mouse click and keyboard event by means of an onmouseup handler.
        > For mouse click, submission is allowed to proceed as normal with the
        > browser appending mouse coordinates. Following a keyboard event,
        > however, submission is cancelled and a timer used to call programmatic
        > submission using form.submit functionality.
        >
        > Using one of your ideas, the programmatic submit function dynamically
        > adds hidden form fields named "x" and "y" with appropriate values before
        > calling form.submit, avoiding duplicate entries for x and y values in
        > the process. Appears to check out in IE5, 6, NS6 and Mozilla.
        >
        > Submitting in the background (without focus) could prove difficult.
        > Personally I leave confirm dialogs enabled for form submission and would
        > definitely cancel a request generated whilst away from a browser window
        > - but that's me.
        >
        > HTH
        > Dom
        >
        > ======== test code =========
        >[/color]
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
        <html>
        <head>
        <title>Untitl ed strict.dtd</title>
        <script type="text/javascript">

        var imgClicked=fals e; // set by mouseup

        function imageSubmit()
        {
        if(imgClicked)
        { imgClicked = false; // in case of later cancellation
        return true;
        }
        window.setTimeo ut("progSubmit( )",50); // don't submit on this event
        return false;
        }
        function progSubmit()
        {
        var form = document.forms. testForm;
        alert("progSubm it()");
        var el = document.create Element("INPUT" );
        el.type="hidden ";
        el.name="x";
        el.value="5"; // whatever
        form.appendChil d(el);
        el = document.create Element("INPUT" );
        el.type="hidden ";
        el.name="y";
        el.value="8"; // idem
        form.appendChil d(el);
        form.submit();
        return true;
        }

        </script>
        </head>
        <body>
        <form name="testForm" id="testForm" action="test.ht ml">
        <input type="image" src="whatever.p ng" id=myImg
        onmouseup="imgC licked=true";
        onclick="return imageSubmit()">
        </form>
        </body>
        </html>

        Comment

        • Roman Bystritskiy

          #5
          Re: Programatically click &lt;INPUT type=image ...&gt; element

          May be you should try to call submit() on your form.

          Say you form id is "theform",
          so in the end of your click handler
          you write document.forms["theform"].submit();

          "Csaba2000" <news@CsabaGabo r.com> wrote in message
          news:bgmpbr$36p @dispatch.conce ntric.net...[color=blue]
          > I want to be able to programatically click on the center of an <INPUT[/color]
          type=image ...> element (I only care about IE[color=blue]
          > 5.5+). This should work regardless of whether IE has focus. Normally you[/color]
          would do myDomElement.cl ick and the mouse[color=blue]
          > doesn't matter, but in the case of an input image element, what happens is[/color]
          the submitted url has something like[color=blue]
          > "?x=12&y=7" appended to it (the numbers vary per mouse position on the[/color]
          clicked element). If you hit the spacebar[color=blue]
          > while that element has focus or do the .click path, then you wind up with[/color]
          "?x=0&y=0" appended to the submitted url,[color=blue]
          > which is no good for me.
          >
          >
          > One possible idea that I have not gotten to work is to create an[/color]
          approriately named hidden element (in our case[color=blue]
          > <input type=hidden name=x> and <input type=hidden name=y>) and set up an[/color]
          onClick handler and insert the position[color=blue]
          > values that way [document.all.x. value = myImageElement. offsetWidth / 2].[/color]
          However, if I insert this element at the[color=blue]
          > end of the form, what happens then is the submitted url's string has a[/color]
          string like "?x=14&x=0& y=0" appended, which is[color=blue]
          > not good enough (for example, PHP will eat the x=14, and who's to say what[/color]
          other web servers do?) The situation is[color=blue]
          > even worse if I insert the hidden element at the beginning of the form,[/color]
          because then the initial click handler will[color=blue]
          > set the hidden element's value, but then quietly dies. Perhaps this[/color]
          method can be patched up, though.[color=blue]
          >
          > The other idea that I had was to use .click with .fireEvent as the code[/color]
          below illustrates (it can also be used to[color=blue]
          > test the above hidden element idea). However, while I am able to modify[/color]
          the click event, the onSubmit event's values[color=blue]
          > don't change. In particular, if I position the mouse above the image[/color]
          element before pressing the "Click me" button,[color=blue]
          > then the mouse coordinates for the onSubmit event appear correct, but the[/color]
          final string comes out with 0's. Similar[color=blue]
          > comments apply if you press the space bar while the focus is on the image[/color]
          element.[color=blue]
          >
          > Thanks,
          > Csaba Gabor from New York
          >
          > <HTML>
          > <HEAD>
          > <TITLE>fireEven t testing</TITLE>
          > <SCRIPT>
          > var TmpOnClick; // a unique temporary variable
          > function clickHandler() {
          > // clickHandler is supposed to rewrite the x,y coordinates of the mouse[/color]
          click[color=blue]
          > var myButton = document.getEle mentsByTagName( "INPUT")[0];
          > var newEvent = document.create EventObject();
          >
          > newEvent.offset X = myButton.offset Width / 2; // middle of image
          > newEvent.offset Y = myButton.offset Height / 2; // middle of image
          > newEvent.cancel Bubble = false;
          > newEvent.button = 1;
          > myButton.onclic k = window.TmpOnCli ck; // Replace the original click[/color]
          handler[color=blue]
          > myButton.fireEv ent ("onclick", newEvent); // Allow the original[/color]
          handler to take over[color=blue]
          > event.cancelBub ble = true; // Cancel the on hold[/color]
          original event[color=blue]
          > }
          >
          > function clickMeSub() {
          > var myButton = document.getEle mentsByTagName( "INPUT")[0];
          > window.TmpOnCli ck = myButton.onclic k; // Save any old click handler
          > myButton.onclic k = clickHandler // Insert our click handler
          > myButton.click( ); // Now invoke it
          > }
          > </SCRIPT>
          > </HEAD>
          > <BODY bgcolor=yellow style=margin-left:.4in>
          > <FORM onsubmit="alert ('submit fired:[/color]
          '+event.offsetX +','+event.offs etY);return true;">[color=blue]
          > <INPUT type=image
          > onclick="alert( 'on click fired: '+event.offsetX +','+event.offs etY);return[/color]
          true;"[color=blue]
          > title="Click with mouse to see both onClick and onSubmit fire."
          > accesskey=s value=SubmitMe>
          > &nbsp;
          > <BUTTON onclick="clickM eSub();return false;"
          > title="Simulate s a click event, but fails to submit mouse coordinates."
          > accesskey=c><u> C</u>lick me</BUTTON>
          > </FORM>
          > </BODY>
          > </HTML>
          >
          >[/color]


          Comment

          Working...