#1
  1. Perl Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    May 2003
    Location
    the far end of town where the Grickle-grass grows
    Posts
    1,860
    Rep Power
    109

    Automatic click link/button


    I'm trying to automate clicking a link, but it seems IE/Firefox only runs the onclick event handlers, but doesn't actually follow the href. Opera and Safari do both, which is what I'm after.

    Here's a test page with code:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>test</title>
    <script type="text/javascript">
    <!--
    function clickElement(elementid){
        var e = document.getElementById(elementid);
        if (typeof e == 'object') {
            /*if(typeof e.click != 'undefined') {
                e.click();
                alert('click');
                return false;
            }
            else */
            if(document.createEvent) {
                var evObj = document.createEvent('MouseEvents');
                evObj.initEvent('click',true,true);
                e.dispatchEvent(evObj);
                alert('createEvent');
                return false;
            }
            else if(document.createEventObject) {
                e.fireEvent('onclick');
                alert('createEventObject');
                return false;
            }
            else {
                e.click();
                alert('click');
                return false;
            }
        }
    }
    //-->
    </script>
    </head>
    <body>
    <p><a id="foo" href="javascript:alert('href')" 
        onclick="alert('onclick')">programatically click me</a></p>
    <p><a href="#" onclick="clickElement('foo')">click me for real</a></p>
    </body>
    </html>
    IE (versions 6 & 7) and Firefox (version 2) don't pop up "href" like Opera and Safari do. Uncommenting that first chunk uses the old click() method, which still exists in IE, but not Firefox. That probably also means that IE won't run more than one event handler, either.

    Does anyone have any suggestions how this might be possible? I hate browser sniffing, so I don't want to check if it's IE or Firefox and force an href location.
    Last edited by icrf; February 25th, 2008 at 12:09 PM.
    Andrew - Perl (and VB.NET) Monkey

    Never underestimate the bandwidth of a hatchback full of tapes.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    96
    Rep Power
    11
    Not quit sure what you are trying to accomplish; could you try to give me an overview of what you are trying to do? Are you trying to access the <a> attributes?
  4. #3
  5. Perl Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    May 2003
    Location
    the far end of town where the Grickle-grass grows
    Posts
    1,860
    Rep Power
    109
    I develop ASP.NET web pages. Much of it tends to be server-side event based, and the events are passed via some generated javascript and hidden variables that are set when clicking a particular button to submit the form. If that generated javascript isn't run, then the postback to the server doesn't know which button was clicked and which click event handler to run.

    My problem is on short forms where the user is likely to just press "enter" from a text box. Most browsers will submit the form when that's pressed, but that doesn't work since the javascript for the button didn't run. The click event handler on the server doesn't execute because the server sees no button as clicked. What I'm doing is adding a javascript keypress event handler, and catching the char(13) and then using javascript, I'm trying to click the proper button. This would make sure any built-in validation runs and everything needed for the back-end is set.

    I'm trying to make something very generic I could use on the various places I need the functionality and not page-by-page specific. I thought there'd be a reasonable way to just simulate a click event.

    I'm willing to go the other way and check which tag type was clicked and handle a submit button differently from an anchor tag. I'm aiming for consistent behavior within each tag type. If I can get Opera and Safari to ignore the href like IE and Firefox, that's just as good. I just really don't like browser sniffing, as it breaks with specific older versions and future updates. Checking functionality is much more stable.

    Any clearer?
    Andrew - Perl (and VB.NET) Monkey

    Never underestimate the bandwidth of a hatchback full of tapes.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    96
    Rep Power
    11
    I am not quit sure how to accomplish what you are wanting to do client side, but if it was me; I would just validate the button value sever side - that way you can tell if it actually was clicked or not. I am not a ASP.Net guy, so I will have to give you an example with PHP (hope you can understand it and convert it to ASP.Net).

    First of all I would prevent my form from being able to be submitted when the enter key was pressed. You can block the keyCode for the "Enter" key or you get do it a different way (which is the way I will be doing it - lol). Then I would give my submit button a name and a value (of course - lol); so that my server side script could validate that my form had actually been submitted.

    Example (HTML/JavaScript)

    Code:
    <script language="javascript">
    function sendIt()
    {
    document.myform.submit();
    document.myform.onsubmit=function() {
    return true;
    }
    }
    </script>
    
    <form name="myform" action="mypage.asp" method="post" onsubmit="return false">
    <input type="text" name="Name">
    <input type="button" name="Submission" value="Submit" onclick="sendIt()">
    </form>

    Example (PHP)

    Code:
    <?php
    
    $button = $_POST['Submission'];
    
    if (isset($button == "Submit")
    {
    // Do Stuff Here
    }
    
    ?>
    Sorry I couldn't give you an example in ASP.Net and this may not be exactly what you were wanting to do; but I hope it is close.

    - Good Luck
  8. #5
  9. Perl Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    May 2003
    Location
    the far end of town where the Grickle-grass grows
    Posts
    1,860
    Rep Power
    109
    That's the rough theory I'm going for, but I'm trying to work with all the ASP.NET-generated javascript and server-side code, not around it. That's why I started the question pretty specific to my problem without getting into the back-end reasoning.

    So I'm back to can I suppress the href following of Opera and Safari, or can I force a follow for Firefox and IE? My best guess otherwise is to try to grab all the onclick event handlers and call each of them manually, and then call the href manually if they all pass as true.
    Andrew - Perl (and VB.NET) Monkey

    Never underestimate the bandwidth of a hatchback full of tapes.

IMN logo majestic logo threadwatch logo seochat tools logo