0

I have this code, where data is DWR object that contains rows from response servlet. img.onclick works using IE9 but I need that it works on IE8 too. Some idea? Thanks!

function functionA(data){
  var getPrintCred = function(data) { 
                        var img = document.createElement("img");
                        img.src = "images/image1.jpg";
                        img.style.width="20px";
                        img.style.height="18px";
                        img.alt = data.field1;
                        img.title = getRegimen;
                        img.onclick = function(target) { functionB(target) };
                        return img;
  };
}

function functionB(data){
                var var1= data.target.title;
                var var2= data.target.alt;
                if ( var1 != null && var1 == "IM")
                    var1 = "valueA";
                else
                    var1 = "valueB";
                functionC(var2,var1);
}

function functionC(param1, param2){
 alert ('Using IE 9 works, but IE8 no works...help me!'+param1+'-'param2);
}
  • 1
    Why do you wrap `functionB` for onclick. Can't it just be: `img.onclick = functionB`? Won't fix the problem, but it makes the code cleaner. – beatgammit Nov 26 '12 at 03:28
  • getPrintCred is local to functionA and goes out of scope when functionA is done executing. Why even do anything with it when nothing changes. – Travis J Nov 26 '12 at 03:29
  • possible duplicate of [Javascript IE Event](http://stackoverflow.com/questions/960419/javascript-ie-event) – Felix Kling Nov 26 '12 at 05:08

2 Answers2

1

Ie 8 does not support the target property in the event object, you have to use srcElement property.

function functionB(data){
            var var1= (data.target || data.srcElement).title;
            var var2= data.target.alt;
            if ( var1 != null && var1 == "IM")
                var1 = "valueA";
            else
                var1 = "valueB";
            functionC(var2,var1);
}

Also it looks like no event object is passed to the onclick event handler, so you can pass window.event as a fallback.

img.onclick = function(target) { functionB(target || event) };

http://jsfiddle.net/mowglisanu/wkB6K/

Musa
  • 96,336
  • 17
  • 118
  • 137
0

Try the below one

function functionA(data){
    var getPrintCred = function(data) { 
        var img = document.createElement("img");
        img.src = "images/image1.jpg";
        img.style.width="20px";
        img.style.height="18px";
        img.alt = data.field1;
        img.title = getRegimen;
        img.onclick = function(target) {
            target = target || window.event;
            functionB(target);
        };
        return img;
    };
}

function functionB(data){
    var var1= data.target.title;
    var var2= data.target.alt;
    if ( var1 != null && var1 == "IM")
        var1 = "valueA";
    else
        var1 = "valueB";
    functionC(var2,var1);
}

function functionC(param1, param2){
    alert ('Using IE 9 works, but IE8 no works...help me!'+param1+'-'param2);
}

In IE the event object is not passed as an argument to the event handler method, but it is available in the global property window.event object.

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531