I have a href
from which I am giving out a popup, and I want to activate this in my body's onload
. I use document.getElementbyId("myelement").click()
which works fine in IE but Firefox doesn't support it. I know that .click()
will not be supported by Firefox , but I don't have any other way to do it.
Can you please suggest a workaround that works in IE as well as in Firefox?
I have a href
from which I am giving out a popup, and I want to activate this in my body's onload
. I use document.getElementbyId("myelement").click()
which works fine in IE but Firefox doesn't support it. I know that .click()
will not be supported by Firefox , but I don't have any other way to do it.
Can you please suggest a workaround that works in IE as well as in Firefox?
Share Improve this question edited Jul 28, 2019 at 21:45 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 15, 2010 at 10:08 SundharSundhar 1491 gold badge7 silver badges21 bronze badges 1-
I know that
.click()
will not be supported by Firefox Some reference would be nice... – Piotr Dobrogost Commented Nov 25, 2012 at 20:58
3 Answers
Reset to default 6You can do as Alsciende suggests, or if you need the event object you can use dispatchEvent
to 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's sometimes useful to use a framework, such as jQuery, to handle these sorts of browser inconsistencies for you. More or less, the same code in jQuery would be:
$('#element').click();
Create a function that opens the popup, and use this function in the body onload handler as well as in the link onclick handler.
function openPopup() {
window.open("...");
return false;
}
<body onload="openPopup()">
...
<a id="myelement" href="#" onclick="openPopup()">...</a>
...
</body>
Just so that it helps someone , i have seen this in use in many code, but can't guarantee if it works in your case , following is what i have tried too -
'leftclick .divname' : '_functionname',
Not sure . but this might be using something that underscore js has to offer . I am using this in a backbone app which has underscore js support .