I want to to automatically click the link when the page is loaded.

I asked about the same a couple of days ago. Attached you will find my solution.

The only tricky thing might be getting the correct node to click (because there is no "id"-attribute).


Assuming that there might be something like

   <div id="MyDIV">
   <a href="#" onClick="return sortTable( 'sortById', 'byId' )"><img
   src="/sortById.gif" alt="Sort by ID" border="0" /></a>
   </div>

you can do it like

   $click( $( '<a', $( '#MyDIV' ) )[0] );

respectivly

   var div = $( '#MyDIV' );
   var a   = $( '<a', div );
   $click( a );




Hope it helps,
Robert


--
You received this message because you are subscribed to the Google Groups 
"greasemonkey-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/greasemonkey-users?hl=en.

--- Begin Message --- I have written a more generic function the perfectly fits my needs. So if someone is interessted, feel free to use it.


    // simulate mouse click on element
    function $click( elm, root ) {
        // create mouseclick element
        var evt = document.createEvent( 'MouseEvents' );
        evt.initEvent( 'click', true, true );

        // id, class, tag, name or node itself?
        var node;
        if( typeof elm == 'string' ) {
            if( elm[0] == '#' ) node = $( elm, root )
            else                node = $( elm, root )[0];
        } else                  node = elm;

        // click node
        node.dispatchEvent( evt );
    }


    // Get Elements
    function $( q, root, single ) {
        if( root && typeof root == 'string' ) {
            root = $( root, null, true );
            if( !root ) { return null; }
        }

        root = root || document;
        if( q[0] == '#' ) { return root.getElementById(q.substr(1)); }
        else if( q[0] == '/' || ( q[0] == '.' && q[1] == '/' ) ) {
if( single ) { return document.evaluate(q, root, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; } return document.evaluate(q, root, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); } else if( q[0] == '.' ) { return root.getElementsByClassName(q.substr(1)); } else if( q[0] == ':' ) { return root.getElementsByName( q.substr(1)); } else if( q[0] == '<' ) { return root.getElementsByTagName( q.substr(1)); }

        return root.getElementsByTagName(q);
    }


Regards,
Robert

-------- Original-Nachricht --------
Betreff: Re: [greasemonkey-users] Acting like a user?
Von: Robert <[email protected]>
An: [email protected]
Datum: Thu Jan 19 2012 21:42:36 GMT+0100
Ah! Great! Tnx!

This seems to be what I want:

    var evt = document.createEvent("MouseEvents");
    evt.initEvent("click", true, true);
    el.dispatchEvent(evt);


Cheers,
Robert


-------- Original-Nachricht --------
Betreff: Re: [greasemonkey-users] Acting like a user?
Von: Anthony Lieuallen <[email protected]>
An: [email protected]
Datum: Thu Jan 19 2012 21:29:37 GMT+0100
http://wiki.greasespot.net/Generate_Click_Events

--
You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.

--

  _ oRobert H.  <mailto:[email protected]>        http://kurzlink.de/Enkidu70
 |<)_/#
 TT<T   [email protected]  <mailto:[email protected]>


"Du wirst da hinein geboren was Du nicht bist, um zu erfahren wer Du wirklich bist!"

--
You received this message because you are subscribed to the Google Groups 
"greasemonkey-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/greasemonkey-users?hl=en.


--- End Message ---

Reply via email to