www.webdeveloper.com
Results 1 to 6 of 6

Thread: Trigger a click on a link

  1. #1
    Join Date
    Feb 2008
    Posts
    2

    Trigger a click on a link

    I'm trying to trigger a click on an A tag from JavaScript. I'd like this to trigger any JavaScript events that might be attached. It looks like Internet Explorer and Opera support a click() function on the link, but FireFox and Safari don't. Does anyone know how I can make this work?

  2. #2
    Join Date
    Oct 2006
    Location
    Ontario Canada
    Posts
    1,160
    use onclick() for FF.
    Here is a sample:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    
    <title>Trigger Link</title>
    
    <script>
    function triggerLink()
    {
    	var el = document.getElementById("linkToGoogle");
    	el.onclick()
    }
    </script>
    </head>
    
    <body onload="triggerLink()">
    
    <a href="http://www.google.com" id="linkToGoogle" onclick="alert('Hello World');"> Link To Google</a>
    
    </body>
    </html>
    I tested it in IE7 also and it works fine.. See if it works in IE6 and Safari

    Drew

  3. #3
    Join Date
    Jan 2008
    Location
    Florida
    Posts
    1,227
    <a href="whatever.html" onclick="myfunction(); return false;">

  4. #4
    Join Date
    Feb 2006
    Posts
    2,926
    el.onclick will only work if the event handlers were set using el.onclick=whatever.

    If the handler was added using addEventListener you have to create and dispatch a mouse event to el.

  5. #5
    Join Date
    Feb 2008
    Posts
    2
    cgishack: I tested it, and mrhoo was right, so that won't work for me.

    yamaharuss: I'm trying to trigger a click, not catch one.

    mrhoo: Thank you. That's pretty much what I ended up doing. I wrote some JavaScript that adds a click() function to the prototype for HTMLAnchorElement if there isn't already one there. The code in the function creates and dispatches the event.


    Here's what I went with:
    Code:
    if (!HTMLAnchorElement.prototype.click) {
      HTMLAnchorElement.prototype.click = function() {
        var ev = document.createEvent('MouseEvents');
        ev.initEvent('click',true,true);
        if (this.dispatchEvent(ev) !== false) {
          //safari will have already done this, but I'm not sniffing safari
          //just in case they might in the future fix it; I figure it's better
          //to trigger the action twice than risk not triggering it at all
          document.location.href = this.href;
        }
      }
    }
    This way, I can simply call click() on the relevant link, whether the browser added it or not.

  6. #6
    Join Date
    Jul 2010
    Posts
    1

    Thumbs up Thanks Mrhoo

    Really appreciate your help for providing the code that registers the click event for the browser. Excellent Work.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles