Forum Moderators: open

Message Too Old, No Replies

Unobtrustive Javascript in IE and srcElement

         

timothymichaelclark

4:10 pm on Mar 24, 2005 (gmt 0)

10+ Year Member



First. Hi, I'm Tim, and this is my first post. You seem like a knowledgable bunch, and I've been tearing out my hair over this problem for a day or two now. I've brought it down to the simplest example possible.

When switching to unobtrusive javascript in IE, I can't seem to access the srcElement of an event anymore.

Example:


<!-- example 1 - this works in ie -->
<html>
<head>
<title>Page</title>
<script>
function testFunction(event) {
alert("event.srcElement.id="+event.srcElement.id);
}
</script>
</head>
<body>
<img id="testimage" src='someimage.jpg' onclick="testFunction(event)" />
</body>
</html>


<!-- example 2 - this doesn't work in ie, says srcElement cannot be found-->
<html>
<head>
<title>Page</title>
<script>
window.onload = function() {
var node = document.getElementById("testimage");
node.onclick=testFunction;
}
function testFunction(event) {
alert("event.srcElement.id="+event.srcElement.id);
}
</script>
</head>
<body>
<img id="testimage" src='someimage.jpg' />
</body>
</html>

I assumed that the two techniques would be synonymous, but apparently not. Can anyone help shed some light on why this is happening, and maybe give a (hopefully unobtrusive) solution? Thanks in advance.

adni18

6:30 pm on Mar 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function testFunction(event) {

I doubt that the event attribute would be passed to the function in IE.

Also, try changing this argument name to "e" (it may be confusing IE)

When you declare the onclick, try something like this:

if(navigator.appName=="Microsoft Internet Explorer")
{
node.onclick=new Function("testFunction(event)");
}
else
{
node.onclick=testFunction;
}

Bernard Marx

12:18 pm on Mar 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The event models are completely different between IE and standards browsers. In IE, 'event' is a global property that holds the current event. As Adni says, using that particular name for the function's parameter will shadow the global property name, making it unavailable.

In standards browsers, the event object is passed as an argument to functions assigned as event handlers. The exception is when the event handler is inline. In this case, you need to use the keyname, event, in the event handler to place the event. For this coincedental reason, the syntax in inline event handlers is the same in all browsers.

Standards event objects don't have a srcElement property, instead they have a target property. It's worth noting that this is a node, not specifically an element, so it may return a text node in some cases. No risk of that here.

!* Change ¦ symbol to unbroken pipe (or it won't work) *!

window.onload = function() { 
document.getElementById("testimage").onclick=testFunction;
}

function testFunction(e) {
// e isn't passed by IE
e = e¦¦event;
alert("The source element's id is: "+ (e.srcElement¦¦e.target).id)
}

timothymichaelclark

2:40 pm on Mar 25, 2005 (gmt 0)

10+ Year Member



Hey guys,

Thanks for your responses. I ended up with the following solution.

<!-- example 3 - unobtrusive - works in ie -->
<html>
<head>
<title>Page</title>
<script>
window.onload = function() {
var node = document.getElementById("testimage");
node.onclick=testFunction;
}
function testFunction(evt) {

if(evt)
alert("evt.target.id="+evt.target.id);
else //IE
alert("window.event.srcElement.id="+window.event.srcElement.id);
}
</script>

</head>

<body>
<img id="testimage" src='someimage.jpg' />
</body>
</html>