Simulating click on link not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maaravi
    New Member
    • Nov 2007
    • 4

    Simulating click on link not working

    Hi,

    I'm trying to simulate clicking on a link, but Firefox seems to disable it.
    In the sample below, when clicking on the div tag, the onclick event of the link is triggered, but the browser doesn't move to the new location.

    could it have to do with security measures?

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Untitled Page</title>
    <script type="text/javascript">
        function simulateClick(el) 
        {
            var evt;
            if (document.createEvent){
                evt = document.createEvent("MouseEvents");
                evt.initMouseEvent("click", false, false, el.ownerDocument.defaultView, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                //evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
            }
            //(evt)? el.dispatchEvent(evt):(el.click && el.click());
            var check = el.dispatchEvent(evt);
        }
    </script>
        
    </head>
    <body>
    <a id="lnk" onclick="alert('clicked')" href="http://www.google.com/">wow</a>
    <div style="width:50px;height:50px; background-color:Black" onclick="simulateClick(document.getElementById('lnk'));"></div>
    </body>
    </html>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    The fourth argument to initMouseEvent should be window - see link.

    Comment

    • maaravi
      New Member
      • Nov 2007
      • 4

      #3
      Originally posted by acoder
      The fourth argument to initMouseEvent should be window - see link.
      The "el.ownerDocume nt.defaultView" returns the window object, so it doesn't matter.

      Anyway, it still doesn't work - to be exact, the event is raised (i added the onclick="alert( 'clicked')" to the link to verify), but the browser stays in the same location.

      Any suggestions?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Maybe it's returning false. That's what would prevent the link being followed to the new location.

        Comment

        • maaravi
          New Member
          • Nov 2007
          • 4

          #5
          Originally posted by acoder
          Maybe it's returning false. That's what would prevent the link being followed to the new location.
          I checked it. It's returning true... :-(

          Could it have something with security measures imposed by Firefox? Maybe the browser doesn't allow moving to another location without the user actively clicking on the link? just a thought...

          Comment

          • maaravi
            New Member
            • Nov 2007
            • 4

            #6
            Well... can someone please enlight me?
            Any help would be appreciated...

            Thanks!

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by maaravi
              Could it have something with security measures imposed by Firefox? Maybe the browser doesn't allow moving to another location without the user actively clicking on the link? just a thought...
              I'm not sure, but what you can do is set the location.href to the link's href - seems a bit overkill, but I can't really think of another solution.

              Comment

              • mrhoo
                Contributor
                • Jun 2006
                • 428

                #8
                If you want to simulate an event you need to define the event in the script's scope first.

                To simulate a link click first use link.onclick or addEventListene r/attachEvent to define the link's behavior for a scripted event. Of course if you define the handler with link.onclick you don't need to create a new event- just call link.onclick().

                If you use the second method you have to create the event.

                Comment

                Working...