ca********@gmail.com wrote:
I can't for the life of me figure out how to execute javascript inside
of div that was set using innerHTML or in my case using cloneNode and
replaceChild (not my idea to do this, I'm just fixing it). I have
tried it with and without the defer attribute. It will work fine in
firefox, but not IE.
If it works in Firefox (which version?), you should consider that a happy
coincidence. You are dealing with a proprietary feature here. There is
nothing that guarantees a script that is included this way to be executed.
<script type="text/javascript">
function setInnerHTMLAndExecScript (element, html) {
var newElement = element.cloneNode(false);
You should indent (posted) code with two or four spaces per indentation
level instead. This way, in contrast to the Tab character, it is posted
and displayed uniformly among user agents, which makes it easier to read
(and to adapt). See also <http://www.jibbering.com/faq/#FAQ2_3and the
links therein.
newElement.innerHTML = html;
element.parentNode.replaceChild(newElement,
element);
}
This shallow cloning x as y, setting the content of y, and then replacing x
with y strikes me as being quite inefficient, to say the least. Changing
the content of x in the first place, i.e.
function setInnerHTMLAndExecScript(element, html)
{
element.innerHTML = html;
}
would probably have sufficed. However, one wants to use something
standards-compliant, like
var c;
while ((c = element.firstChild)) element.removeChild(c);
var input = element.appendChild(document.createElement("input" ));
input.type = "button";
input.value = "test me";
_global.testMe = function() {
window.alert("test called");
};
input.addEventListener("click", function() { _global.testMe(); }, false);
instead, whereas `global' can be a reference to the Global Object or to any
other user-defined object. Since (AFAIK) MSHTML does not support
EventTarget::addEventListener(), and addEvent() is buggy, you will need the
equivalent of
input.onclick = function() {
window.alert("test called");
};
as an alternative approach in that case.
HTH
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann