I have a href from that i am giving out a popup , and i want to activate this on my body overload . I am using "document.getElementbyId("myelement").click()" this is working fine in IE but FireFox is not supporting it. And i know that .click() will not be taken by firefox , but i dont have any other way to do it. Can u please suggest me with a workaround that will be accepted by IE as well as FF. Thanks in advance.
A:
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>
Alsciende
2010-06-15 10:12:47
+2
A:
You 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();
Andy E
2010-06-15 10:15:46
Hi Andy E's head , a BIG thanks to you, your solution is working great.
script programmer
2010-06-15 10:37:52
@script programmer: no problem at all :-) You can mark my answer as the correct solution using the tick/check mark just below the current number of votes on the left. This gives me a whopping 15 rep, you 2 rep and improves your acceptance rate :-)
Andy E
2010-06-15 10:41:40
A:
Hi Alsciende, Thanks for the suggestion but window.open() opens the href in a new window but i want that as a overlay in the same page.
script programmer
2010-06-15 10:31:33
To reply to an answer, you need to click the "add comment" link below it. You can add comment replies to your own questions and answers and answers to your questions until you reach 50 reputation. After that you can comment on anyone's questions and answers. You can delete this answer by clicking "delete" next to "link | edit" to the left.
Andy E
2010-06-15 10:40:16
The content of openPopup() was just an example... The point wasn't there.
Alsciende
2010-06-16 07:57:48
A:
Hi Andy E's head , a BIG thanks to you, your solution is working great.
script programmer
2010-06-15 10:38:00