Programatically trigger an onClick event?

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

    Programatically trigger an onClick event?

    Is it possible to use JavaScript to cause the browser to click a link on a
    page and have the browser act exactly like the user had clicked on the link
    directly?

    In other words, I need to programmaticall y issue a JavaScript statement
    which causes the browser to act just like the user clicked on a link in my
    page. And if that link has an onClick JS event defined, I'd want that
    onClick event to execute too, exactly the same as if the user actually
    clicked on the link.

    I know you may be thinking that I should just call the onClick event for the
    link directly rather than trying to simulate a click using JavaScript.
    However this is not sufficient. In my case I need to use JS to cause the
    browser to click a link just like the user had clicked it, have any
    corresponding events that may be defined fire (such as onClick), and then
    have the browser follow the href for the link to its appropriate
    target/page. Think of it as a macro-like operation I am trying to do
    whereby I am simulating an action of and end user that is clicking a link.

    Can someone let me know if this is possible, and if so, how? I need it to
    work with IE 5 and higher, and the later versions of Netscape/Opera/Mozilla
    if possible.

    I'm thinking that someone I may be able to access the properties of an href
    tag programmaticall y and call a method on it to simulate a real click. Any
    code samples or snippets would be MUCH appreciated.

    Thanks!!

    ** Mike



  • Berislav Lopac

    #2
    Re: Programatically trigger an onClick event?

    Mike Gratee wrote:[color=blue]
    > Is it possible to use JavaScript to cause the browser to click a link
    > on a page and have the browser act exactly like the user had clicked
    > on the link directly?[/color]

    var link = document.getEle mentById['yourLinksIdAtt rbuteValue'];
    link.click();

    --
    If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
    Groucho, Chico, and Harpo, then Usenet is Zeppo.


    Comment

    • Mike Gratee

      #3
      Re: Programatically trigger an onClick event?

      Great - thanks alot. However I don't think that will work with Netscape 6,
      Mozilla or Opera right? How would I change it to work with those?

      Mike

      "Berislav Lopac" <berislav.lopac @dimedia.hr> wrote in message
      news:cd55ve$1rj $1@ls219.htnet. hr...[color=blue]
      > Mike Gratee wrote:[color=green]
      > > Is it possible to use JavaScript to cause the browser to click a link
      > > on a page and have the browser act exactly like the user had clicked
      > > on the link directly?[/color]
      >
      > var link = document.getEle mentById['yourLinksIdAtt rbuteValue'];
      > link.click();
      >
      > --
      > If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
      > Groucho, Chico, and Harpo, then Usenet is Zeppo.
      >
      >[/color]


      Comment

      • Berislav Lopac

        #4
        Re: Programatically trigger an onClick event?

        Mike Gratee wrote:[color=blue][color=green]
        >> var link = document.getEle mentById['yourLinksIdAtt rbuteValue'];
        >> link.click();[/color]
        >
        > Great - thanks alot. However I don't think that will work with
        > Netscape 6, Mozilla or Opera right? How would I change it to work
        > with those?[/color]

        Have you tried? If not, why do you think if it won't work? If yes, what
        exactly happens?

        Berislav

        --
        If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
        Groucho, Chico, and Harpo, then Usenet is Zeppo.


        Comment

        • Mike Gratee

          #5
          Re: Programatically trigger an onClick event?

          I was under the impression that getElementById would not work like this with
          some of the older browsers. Wasn't there a different approach to getting to
          an element under older versions of Netscape and Opera? What is that
          approach? What versions of Netscape and Opera started supporting
          getElementById? Thank you!

          Mike

          "Berislav Lopac" <berislav.lopac @dimedia.hr> wrote in message
          news:cd7ubv$lsr $1@ls219.htnet. hr...[color=blue]
          > Mike Gratee wrote:[color=green][color=darkred]
          > >> var link = document.getEle mentById['yourLinksIdAtt rbuteValue'];
          > >> link.click();[/color]
          > >
          > > Great - thanks alot. However I don't think that will work with
          > > Netscape 6, Mozilla or Opera right? How would I change it to work
          > > with those?[/color]
          >
          > Have you tried? If not, why do you think if it won't work? If yes, what
          > exactly happens?
          >
          > Berislav
          >
          > --
          > If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
          > Groucho, Chico, and Harpo, then Usenet is Zeppo.
          >
          >[/color]


          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Programatically trigger an onClick event?

            "Mike Gratee" <nospam@me.or g> writes:

            A previous poster wrote:[color=blue][color=green][color=darkred]
            >>>> var link = document.getEle mentById['yourLinksIdAtt rbuteValue'];[/color][/color][/color]

            That should be:
            var link = document.getEle mentById('yourL inksIdAttrbuteV alue');
            getElementById is a function, not an array.
            [color=blue]
            > I was under the impression that getElementById would not work like
            > this with some of the older browsers. Wasn't there a different
            > approach to getting to an element under older versions of Netscape
            > and Opera? What is that approach?[/color]

            Netscape 4 had document.layers , a collection which only contains
            <layer> elements and <div> elements with CSS position:absolu te.
            [color=blue]
            > What versions of Netscape and Opera started supporting
            > getElementById?[/color]

            Netscape: version 6 (after basing it on the Mozilla project).
            Opera: version 4


            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Henry Lafleur

              #7
              Re: Programatically trigger an onClick event?

              "Berislav Lopac" <berislav.lopac @dimedia.hr> wrote in message news:<cd7ubv$ls r$1@ls219.htnet .hr>...[color=blue]
              > Mike Gratee wrote:[color=green][color=darkred]
              > >> var link = document.getEle mentById['yourLinksIdAtt rbuteValue'];
              > >> link.click();[/color]
              > >
              > > Great - thanks alot. However I don't think that will work with
              > > Netscape 6, Mozilla or Opera right? How would I change it to work
              > > with those?[/color]
              >
              > Have you tried? If not, why do you think if it won't work? If yes, what
              > exactly happens?
              >
              > Berislav[/color]

              It does not work with FireFox 9.1 (latest) which runs Mozilla Gecko engine:

              Error: lnkNode.click is not a function
              Source File: http://blah/WebApplication1/TreeNode.js
              Line: 250

              Works fine in IE.

              Searching for an answer...


              Henry.

              Comment

              • Grant Wagner

                #8
                Re: Programatically trigger an onClick event?

                Henry Lafleur wrote:
                [color=blue]
                > "Berislav Lopac" <berislav.lopac @dimedia.hr> wrote in message news:<cd7ubv$ls r$1@ls219.htnet .hr>...[color=green]
                > > Mike Gratee wrote:[color=darkred]
                > > >> var link = document.getEle mentById['yourLinksIdAtt rbuteValue'];
                > > >> link.click();
                > > >
                > > > Great - thanks alot. However I don't think that will work with
                > > > Netscape 6, Mozilla or Opera right? How would I change it to work
                > > > with those?[/color]
                > >
                > > Have you tried? If not, why do you think if it won't work? If yes, what
                > > exactly happens?
                > >
                > > Berislav[/color]
                >
                > It does not work with FireFox 9.1 (latest) which runs Mozilla Gecko engine:
                >
                > Error: lnkNode.click is not a function
                > Source File: http://blah/WebApplication1/TreeNode.js
                > Line: 250
                >
                > Works fine in IE.
                >
                > Searching for an answer...
                >
                > Henry.[/color]

                This might work for you:

                <a href="http://www.yahoo.com" onclick="alert( 'hi');return true;">Yahoo!</a>
                <form>
                <input type="button" onclick="naviga teLink(0);" value="Click link index 0">
                </form>
                <script type="text/javascript">
                function navigateLink(li nkIndex) {
                var link = document.links[linkIndex];
                if (link.onclick && link.onclick()) {
                if (link.target) {
                window.open(lin k.href, link.target);
                } else {
                window.location .href = link.href;
                }
                }
                }
                </script>

                Of course it won't completely mimic the user clicking the link, if the link has a target and Javascript
                is disabled, a user-initiated click would still open a new window (assuming that TARGET attributes are
                honored by the user agent). Also, a TARGET attribute probably has a better chance of opening a new
                window then window.open() does, given the current state of popup blockers.

                --
                Grant Wagner <gwagner@agrico reunited.com>
                comp.lang.javas cript FAQ - http://jibbering.com/faq

                Comment

                • Henry Lafleur

                  #9
                  Re: Programatically trigger an onClick event?

                  "Berislav Lopac" <berislav.lopac @dimedia.hr> wrote in message news:<cd7ubv$ls r$1@ls219.htnet .hr>...[color=blue]
                  > Mike Gratee wrote:[color=green][color=darkred]
                  > >> var link = document.getEle mentById['yourLinksIdAtt rbuteValue'];
                  > >> link.click();[/color]
                  > >
                  > > Great - thanks alot. However I don't think that will work with
                  > > Netscape 6, Mozilla or Opera right? How would I change it to work
                  > > with those?[/color]
                  >
                  > Have you tried? If not, why do you think if it won't work? If yes, what
                  > exactly happens?
                  >
                  > Berislav[/color]

                  Here's a solution (should handle IE and Netscape 6+/Mozilla
                  1.2/FireFox):

                  /**
                  * Emulate the IE link.click method.
                  * plnkNode - The link object to emulate the click on.
                  */
                  function MozillaLinkClic k(plnkNode)
                  {
                  // Emulate click on Mozilla
                  var bolFollowLink = true;
                  var strTarget = null;

                  // Run the link onClick event.
                  if (plnkNode.oncli ck)
                  {
                  if (plnkNode.oncli ck() == false)
                  {
                  bolFollowLink = false;
                  }
                  }

                  // Get the link target.
                  if (plnkNode.targe t)
                  {
                  strTarget = plnkNode.target ;
                  }
                  if (bolFollowLink)
                  {
                  if (strTarget == null)
                  {
                  // Open using the default target if not set on the link.
                  window.open(pln kNode.href);
                  } else {
                  // Open using the link target.
                  window.open(pln kNode.href, strTarget);
                  }
                  }
                  }

                  // Assume a link object called Link:
                  if (Link.click)
                  Link.click();
                  else
                  MozillaLinkClic k(Link);


                  Note that under normal behavior if the onClick event returns false,
                  the link should not be followed.

                  Henry.

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: Programatically trigger an onClick event?

                    Lasse Reichstein Nielsen wrote:[color=blue]
                    > "Mike Gratee" <nospam@me.or g> writes:[color=green][color=darkred]
                    >>>>> var link = document.getEle mentById['yourLinksIdAtt rbuteValue'];[/color][/color]
                    >
                    > That should be:
                    > var link = document.getEle mentById('yourL inksIdAttrbuteV alue');
                    > getElementById is a function, not an array.[/color]

                    The proper correction would have been: "getElementById () is a method,
                    not a non-function property." ECMAScript/J(ava)Script has no concept
                    of associative arrays, the above referencing would be used as bracket
                    property accessor on *any* object.


                    PointedEars

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: Programatically trigger an onClick event?

                      Henry Lafleur wrote:[color=blue]
                      > "Berislav Lopac" <berislav.lopac @dimedia.hr> wrote in message news:<cd7ubv$ls r$1@ls219.htnet .hr>...[/color]

                      Please do not write attribution novels. Duplicating header information
                      of second-level interest is a waste of bandwidth and makes discussions
                      less legible.
                      [color=blue][color=green]
                      >> Mike Gratee wrote:[color=darkred]
                      >>>> var link = document.getEle mentById['yourLinksIdAtt rbuteValue'];
                      >>>> link.click();
                      >>>
                      >>> Great - thanks alot. However I don't think that will work with
                      >>> Netscape 6, Mozilla or Opera right? How would I change it to work
                      >>> with those?[/color]
                      >>
                      >> Have you tried? If not, why do you think if it won't work? If yes, what
                      >> exactly happens?
                      >> [...][/color][/color]

                      Please quote only what you are referring to. Explicitely
                      *do not* quote signatures if you are not referring to them.

                      <http://netmeister.org/news/learn2quote.htm l>
                      [color=blue]
                      > It does not work with FireFox 9.1 (latest) [...][/color]

                      The latest version ist Firefox 0.9.1.
                      [color=blue]
                      > which runs Mozilla Gecko engine:[/color]

                      Firefox uses the Netscape Gecko engine. But Gecko is a *rendering*
                      engine, not a script engine. The script engine used in Mozilla/5.0
                      user agents is SpiderMonkey, a JavaScript 1.5 engine written in C++.
                      It is this engine which puts messages like the following to the
                      JavaScript console:
                      [color=blue]
                      > Error: lnkNode.click is not a function
                      > Source File: http://blah/WebApplication1/TreeNode.js
                      > Line: 250
                      >
                      > Works fine in IE.
                      >
                      > Searching for an answer...[/color]

                      The answer is that Mozilla/5.0 and IE implement different Document
                      Object Models (DOMs). Mozilla/5.0 implements the Gecko DOM, IE
                      implements the IE DOM.

                      <http://www.mozilla.org/docs/dom/>
                      <http://msdn.microsoft. com/workshop/author/dhtml/reference/dhtml_reference _entry.asp>

                      An object or property of one DOM needs not to be available in another
                      and apart of that, different UAs implement different scripting
                      languages. This is why client-side scripting is much more challenging
                      that it at first may seem and good scripts much more sophisticated than
                      the usual copy & pray scripts may (and some even explicitely and falsely
                      do) suggest.

                      <http://pointedears.de/scripts/test/whatami>


                      PointedEars

                      Comment

                      • Frederic Banaszak

                        #12
                        Re: Programatically trigger an onClick event?

                        On Sun, 18 Jul 2004 02:46:12 +0200, Thomas 'PointedEars' Lahn
                        <PointedEars@nu rfuerspam.de> wrote:
                        [color=blue]
                        >
                        >The latest version ist Firefox 0.9.1.
                        >[/color]

                        The latest version is Firefox 0.9.2.

                        Comment

                        • Thomas 'PointedEars' Lahn

                          #13
                          Re: Programatically trigger an onClick event?

                          Frederic Banaszak wrote:[color=blue]
                          > On Sun, 18 Jul 2004 02:46:12 +0200, Thomas 'PointedEars' Lahn
                          > <PointedEars@nu rfuerspam.de> wrote:[/color]

                          Please do not write attribution novels. Reasons have been pointed out
                          before.
                          [color=blue][color=green]
                          >> The latest version ist Firefox 0.9.1.[/color]
                          >
                          > The latest version is Firefox 0.9.2.[/color]

                          With a branch/trunk release scheme for different operating systems and
                          platforms as mozilla.org uses, it remains to be discussed what can be
                          considered the latest version. For me, for example the latest version
                          to date is Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8a3)
                          Gecko/20040717 Firefox/0.9.1+, a GNU/Linux Nightly Build as of
                          yesterday. The latest release is 0.9.2, but available as binary only
                          for 32 bit Windows. The latest public binaries for GNU/Linux and MacOS
                          X are of version 0.9.1.


                          PointedEars

                          Comment

                          • Frederic Banaszak

                            #14
                            Re: Programatically trigger an onClick event?

                            On Sun, 18 Jul 2004 14:55:15 +0200, Thomas 'PointedEars' Lahn
                            <PointedEars@nu rfuerspam.de> wrote:
                            [color=blue]
                            >
                            >Please do not write attribution novels. Reasons have been pointed out
                            >before.
                            >[/color]
                            I have not written any novel, attribution or otherwise. My newsreader
                            does what it does. Since you are the only person ever, in all my years
                            on Usenet, to complain about such a thing, I really do not think that
                            I will be changing it any time soon.

                            [color=blue][color=green][color=darkred]
                            >>> The latest version ist Firefox 0.9.1.[/color]
                            >>
                            >> The latest version is Firefox 0.9.2.[/color]
                            >
                            >With a branch/trunk release scheme for different operating systems and
                            >platforms as mozilla.org uses, it remains to be discussed what can be
                            >considered the latest version. For me, for example the latest version
                            >to date is Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8a3)
                            >Gecko/20040717 Firefox/0.9.1+, a GNU/Linux Nightly Build as of
                            >yesterday. The latest release is 0.9.2, but available as binary only
                            >for 32 bit Windows. The latest public binaries for GNU/Linux and MacOS
                            >X are of version 0.9.1.
                            >[/color]

                            I stand corrected, I suppose, but since you are such a stickler for
                            precision, with one glaring exception, I thought that it should be
                            mentioned that a 0.9.2 version does exist (for Windows).



                            Comment

                            • Dr John Stockton

                              #15
                              Re: Programatically trigger an onClick event?

                              JRS: In article <40FA7333.10009 08@PointedEars. de>, seen in
                              news:comp.lang. javascript, Thomas 'PointedEars' Lahn
                              <PointedEars@nu rfuerspam.de> posted at Sun, 18 Jul 2004 14:55:15 :[color=blue]
                              >Frederic Banaszak wrote:[color=green]
                              >> On Sun, 18 Jul 2004 02:46:12 +0200, Thomas 'PointedEars' Lahn
                              >> <PointedEars@nu rfuerspam.de> wrote:[/color]
                              >
                              >Please do not write attribution novels. Reasons have been pointed out
                              >before.[/color]

                              Incorrectly, which is possibly why people ignore your intolerant
                              demands. AISB, you should read
                              Read more about the Internet Engineering Task Force (IETF), the premier standards development organization (SDO) for the Internet.

                              Read more about the Internet Engineering Task Force (IETF), the premier standards development organization (SDO) for the Internet.

                              in order to learn current thinking on the subject.

                              It is that it is acceptable to include poster's name, E-address, the
                              newsgroup name, and the precursor article's date/time and message-ID.

                              Many people find these helpful under various circumstances - one example
                              is when someone such as yourself resurrects ancient threads without
                              warning

                              I have repeatedly pointed out to you, publicly, the unreasonablenes s of
                              your demands. However, as you claim to have the anti-social habit of
                              kill-filing those who disagree with you, you may not have seen all of
                              those.

                              The net effect, of course, is to make you appear a dictatorial, juvenile
                              prat.


                              Have you noticed how rapidly the number of articles posted to this
                              newsgroup per week has been dropping since you began to post?

                              --
                              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
                              Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
                              Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
                              No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

                              Comment

                              Working...