[thelist] [Javascript] click() not working with Safari

J.J.SOLARI jjsolari at pobox.com
Fri Jun 24 08:02:01 CDT 2005


Christian Heilmann 24/06/05 11:34 +0100:

>What is the rationale of click()?
>
>wouldn't a  
>document.getElementById( "submit" ).onclick=a_func;
>executed onload be cleaner, more supported and easier?
>or even a 
>
><form onsubmit="return a_func();">
>
>Then you don't need to add anything to the HTML at all.

Christian,

As said in the first post (severely trimmed), the whole thing is
about simulating an accesskey-like behaviour (but without any
modifier key) on check boxes, radios and buttons type="submit".
There are only <button type="submit"> and no <input type="submit">.

Actually, all functions are initialized when document has loaded,
and most form elements receive a "click" event listener and so do
the buttons. Using the mouse to activate (ie. click) these elements
works fine.

However, to emulate the accesskey-like behaviour, there is a "keyup"
event listener set on the window element:

function init()
{
    document.getElementById( "a_button" ).onclick = submitForm;
    ...
    window.keyup = accessKeyup;
    ...
}

Function accessKeyup in turn processes the key event (ie. the
accesskey) and here is how the click() method is invoked:

function accessKeyup()
{
    ...
    var button_id = null;
    ...
    // determine which button is activated
    switch( e.keyCode )
    {
        case xx: button_id = "a_button";
        ...
    }
    ...
    if( button_id )
    {
        var submit_ok = null;
        ...
        // various checks to spare Http requests
        switch( button_id )
        {
            // function someButtonClick returns a boolean
            case "a_button": submit_ok = someButtonClick;
            ...
        }
        if( submit_ok )
        {
            document.getElementById( "a_button" ).click();
        }
        ...
    }
    ...
}

This general code is ok with Opera, Firefox, Camino and Safari
(except for click()). I don't know yet about IE5+ so I'll set up a
test page soon.

Besides Safari 2.0 lack of implementation of method click() on the
button element, there is still the solution to use "regular" <input
type="submit">: it works with all above browsers. However, this
solution brings its own problems too: it's easier to style a text
content in buttons than in inputs.

Admittedly, I have not fully explored the submit way on the form
element itself.

Thanks for suggesting it,

JJS.





More information about the thelist mailing list