0

I'm trying to simulate a mouse click event in IE/FireFox browsers when ctrlKey tag is true. I've googled it a lot, but I cannot find why my code isn't working. What supposed to happen: - triggerClick should click the link (that is fine in both IE & FF) - triggerClickCtrl should open the link in a new tab (in IE - it does the same as the buttom triggerClick, and in FF it does nothing). - triggerClickShift should open the link in a new window (in IE - it does the same as the buttom triggerClick, and in FF it does nothing). Note: this code works in chrome, but it doesn't work in IE11 and FireFox.

any suggestions? Thanks.

<!DOCTYPE html>
<html>

<p><a id = 'bingLink' href = "http://www.bing.com">Bing</a></p>

<button onclick=triggerClick()>triggerClick</button>
<button onclick=triggerClickCtrl()>triggerClickCtrl</button>
<button onclick=triggerClickShift()>triggerClickShift</button>

<script>
    function triggerClick()
    {
        simulateClick(false, false, false);
    }
    function triggerClickCtrl()
    {
        simulateClick(true, false, false);
    }
    function triggerClickShift()
    {
        simulateClick(false, true, false);
    }

    var simulateClick = function (ctrl, shift, isMiddle) {
        var evt = document.createEvent('MouseEvents');
        evt.initMouseEvent('click', true, true, window, 0, 0, 0, 10, 10, ctrl, false, shift, false, isMiddle ? 1 : 0, null );
        document.getElementById('bingLink').dispatchEvent(evt);
    }

</script>

</body>

</html>
2
Contributors
11
Replies
24
Views
1 Year
Discussion Span
Last Post by rproffitt
0

I see this question kicking around since about 2011 but no clear answer.

However as I read https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode I don't see an event for the keycode you are sending.

If you wanted to learn what Mozilla is listening for you might have to get into the source but the big clue here why it didn't respond is in the keycode article.

How to fix? May had to similate the control key down, then your mouse click down, up then a control key up.

0

Thanks for you answer.
I didn't send keyboard event for ctrlKey, but sent it inside the mouseEvent.

event.initMouseEvent(type, canBubble, cancelable, view,
                     detail, screenX, screenY, clientX, clientY,
                     ctrlKey, altKey, shiftKey, metaKey,
                     button, relatedTarget);

The function 'triggerClickCtrl' send initMouseEvent with ctrlKey=true. But, as mentioned before, it doesn't seem to work.
I can write mechanism which send keyPress event, but I rather understand why this one isn't working.

0

"understand why this one isn't working."

Remember that this has been kicked around in prior discussions so I went looking for what keycodes Mozilla and IE respond to. The keycode you tried didn't look to be in the article I linked to. Maybe there's another for mouse? Look for a document on that area.

0

I've tried to use MouseEvent() as describe in Mozilla site.

    var simulateClick = function (ctrl, shift, isMiddle) {
        var element = document.getElementById('bingLink');
        var evt = new MouseEvent("click", {
            view: window,
            bubbles: true,
            cancelable: true,
            clientX: 10,
            clientY: 10,
            ctrlKey: true
        });
        element.dispatchEvent(evt);
}

This is the new simulateClick function. But I still get the same result. It doesn't perform the ctrlKey press during click.

0

Even dispatch keyboard (ctrlKey) and mouse (click) wasn't worked.
Any other suggestions?

0

Have a specific one in mind? I dont realy care which link. Just wanted to cause the browser simulate open new tab while clicking a link with ctrl..

0

Hmm, who said the control+click opens a new tab? That result is not assured as I can change that in the browser settings.

As to a specific one, why not this page (Daniweb.com) Try it on the <DANIWEB> link. And remember that browser settings can thwart this action. I can disable, cause it to open tab or window.

That said, why does this need to work in all browsers? Tell some story.

0

I don't really care what will happen when clicking mouse+ctrl. I just want to achieve the same result as manual clicking.
It also doesn't work with this site :\

0

It looks good except in the areas of where anti-robot-clicking is imposed. There's been a lot of work to stop automated clickers since those are not worthy clicks. I was incorrect to tell you to try it here since well, you know why.

You may have to test this on your own local web site that is clean and not going to block fake or automated clicks.

-> Did you try this with AutoIT?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.