E
E
Evgeny Fedorov2014-10-21 13:28:17
JavaScript
Evgeny Fedorov, 2014-10-21 13:28:17

How to simulate a mouse wheel click (js)?

You need to learn how to open a background tab in ie and ff, everything is fine with Chrome.
That is, you need to open a tab like target="_blank" but so that the first tab remains active.
There was an idea to simulate a ctrl + left mouse click, and it worked fine in chrome, but not in fire fox and Internet explorer. Now the idea is to simulate the middle mouse button.
If this does not work, then you need to learn how to change the active tab in the browser through js, that is, we open the page as _blank, in which the code: switch from this tab to the previous one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Finesse, 2014-10-21
@Finesse

This is in theory, in reality, browsers perceive this as a left-click:

// Функция, симулирующая события. obj — объект события, even — название события без приставки on, data — атрибуты события (объекта event)
function triggerEvent(obj, even, data) {
  var event;
  if(document.createEvent) {
    event = document.createEvent("HTMLEvents");
    event.initEvent(even, true, true);
  } else {
    event = document.createEventObject();
    event.eventType = even;
  }
  event.eventName = even;
  if(data)
    for(var i in data)
      event[i] = data[i];
  if(document.createEvent)
    obj.dispatchEvent(event);
  else
    obj.fireEvent("on" + event.eventType, event);
}

// Симуляция нажатия колёсиком мыши
triggerEvent(document.getElementById('button'), 'click', {button: 1});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question