Question

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);
}
Was it helpful?

Solution

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/

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top