eNepper
eNepper

Reputation: 1969

ImageElement OnClick Event

How can I atatch an onclick event to an ImageElement in using ScriptSharp?

I'm getting an error when i'm trying this:

 ImageElement Button = (ImageElement)Document.CreateElement("img");
        Button.Src = "image.png";
        Button.AddEventListener("click", delegate(ElementEvent e)
        {
            Script.Alert("Clicked");
        }, false);

It works when I view the page in Google Chrome, but not in IE8... So I guess that ScriptSharp cant create code to use with IE < 9?

Upvotes: 1

Views: 490

Answers (2)

Nikhil Kothari
Nikhil Kothari

Reputation: 5225

The github issue alsoo covers this - https://github.com/NikhilK/scriptsharp/issues/1

In short, I'd recommend either an existing wrapper, like jQuery, or your own custom one that abstracts the eventing differences and support for DOM events in different browsers, rather than conditionally calling attachEvent from within app code.

In general script#, as the compiler, doesn't do abstraction of differences across browsers - frameworks do that. This is folks either have their own abstractions, or framework choices they've already made, and often opinions about how the abstractions should work.

Upvotes: 1

DuckMaestro
DuckMaestro

Reputation: 15885

This is a problem with how you are using the DOM. IE7 through 8 does not support addEventListener. You need to use attachEvent or a library that wraps the differences between the browsers (such as jQuery).

https://developer.mozilla.org/en/DOM/element.addEventListener

Upvotes: 2

Related Questions