Alternative for .click () method in Firefox

I have href, from which I give out a popup, and I want to activate it in my body onload. I use document.getElementbyId("myelement").click()one that works fine in IE, but Firefox does not support it. I know that .click()Firefox will not be supported, but I have no other way to do this.

Can you suggest a workaround that works in IE as well as in Firefox?

+3
source share
3 answers

You can do what Alsciende offers, or if you need an event object, you can use dispatchEventto trigger an event handler:

document.body.onload = function () {
    var element = document.getElementById("element");

    if ("click" in element)
        element.click();
    else if ("dispatchEvent" in element) {
        var evt = document.createEvent("MouseEvents");
        evt.initMouseEvent("click", true, true, window,
            0, 0, 0, 0, 0, false, false, false, false, 0, null);
        element.dispatchEvent(evt);
    }
}

It is sometimes useful to use a framework like jQuery to deal with these kinds of browser inconsistencies. Moreover, the same code in jQuery will be:

$('#element').click();
+6
source

Create a function that opens a popup window and use this function in the body's onload handler, as well as in the onclick link handler.

function openPopup() {
  window.open("...");
  return false;
}

<body onload="openPopup()">
 ...
 <a id="myelement" href="#" onclick="openPopup()">...</a>
 ...
</body>
+2
source

, - , , , , , , -

'leftclick.divname': '_functionname',

Not sure. but it could be using something that js underscore might offer. I use this in a basic application with js support.

0
source

Source: https://habr.com/ru/post/1750085/


All Articles