简体   繁体   中英

Internet Explorer 8. Events

So I have this website, which was created using a sencha Editor (Sencha Animator).

They don't really support IE for that matter, so I decided to custom edit it because most of our clients still use IE7+.

The main problems are now that it loads fine in Chrome, Safari, FF, and IE8/9 But none of the events seem to work.

I first tried to do some conditional check (which I found here) to see what browser it was and add the events like that:

function bindEvent(el, eventName, eventHandler) {
   if (el.addEventListener){
       el.addEventListener(eventName, eventHandler, false); 
   } else if (el.attachEvent){
       el.attachEvent("on"+eventName, eventHandler);
   }
}

Somehow this code suddenly breaks on

if (el.addEventListener) {

So after that I thought wait, jQuery supposedly has this build in. So I added the CDN for jQuery from google.

in my JS I added the

$(document).ready(function () {
   // load my start function here
});

That works, so atleast the page loads all of it's content.

Now I want to use

$(element).click(function () {
   alert('hello');
});

But that doesn't work at all (nor does mouseup for that matter) in IE nor Firefox (IE works better then FF in this instance)

I have no clue anymore, this whole IE thing breaks about everything, and a site that only works in Chrome is just not possible... this is really really frustrating :P

the link to the site can be found here: Link

here is the complete JS Code:

if (typeof (AN) === 'undefined') {
   AN = {};
}
AN.Controller = {
   scenes: {},
   scenesArray: [],
   currentSceneID: -1,
   olElement: null,
   events: {},
   useOrmma: false,
   setConfig: function (configData) {
      this.events = configData.events;
      this.olElement = document.getElementById(configData.parentId);
      var liElements = this.olElement.children;
      if (configData.ormma) {
        this.useOrmma = true;
    }
    var scene;
    for (var i = 0; i < configData.scenes.length; i++) {
        scene = configData.scenes[i];
        scene.element = liElements[i];
        this.scenes[scene.id] = scene;
        this.scenesArray.push(scene);
    }
    this.setupListeners();
    this.startSceneByID(this.scenesArray[0].id);
},
runningAnimationCount: 0,
browser: 'webkit',
setupListeners: function () {
    var me = this;
    var eventName = "webkitAnimationEnd";
    if (document.body.style.MozAnimationName !== undefined) {
        eventName = "animationend";
        this.browser = "moz";
    }

    function addMousemoveListenerTo(scene) {
        /*bindEvent(scene.element, 'mousemove', function (event) {
            scene.mousemoveAction(me, event);
        }, false);*/

    }
    var scene;
    for (var i = 0; i < this.scenesArray.length; i++) {
        scene = this.scenesArray[i];
        if (scene.mousemoveAction) {
            addMousemoveListenerTo(scene);
        }
    }

    function addListenerTo(element, eventType, aFunction) {
        /*bindEvent(element, eventType, function (event) {
            aFunction(me, event);
        });*/
        $("#AN-sObj-17984").live("click", function(){
            AN.Controller.startSceneByID(2);
        });
    }

    var element, event;
    for (var i = 0; i < this.events.length; i++) {
        event = this.events[i];
        element = document.getElementById(event.id);
        addListenerTo(element, event.type, event.handler);
    }
},
onAnimationEnd: function () {
    this.runningAnimationCount--;
    if (this.runningAnimationCount === 0) {
        this.onSceneFinish();
    }
},
startSceneByID: function (sceneID) {
    var me = this;
    if (sceneID === this.currentSceneID) {
        this.scenes[sceneID].element.setAttribute('class', 'run restart');
        setTimeout(function () {
            me.runningAnimationCount = me.scenes[sceneID].animationCount;
            me.scenes[sceneID].element.setAttribute('class', 'run');
            if (me.scenes[sceneID].startAction) {
                me.scenes[sceneID].startAction(me);
            }
            if (me.scenes[sceneID].animationCount === 0) {
                me.onSceneFinish();
            }
        }, 0);
        return;
    } else if (this.currentSceneID !== -1) {
        this.scenes[this.currentSceneID].element.setAttribute('class', '');
    }
    this.runningAnimationCount = this.scenes[sceneID].animationCount;
    this.currentSceneID = sceneID;
    var nextScene = this.scenes[sceneID];
    if (this.browser === 'moz') {
        nextScene.element.setAttribute('class', 'run restart');
        var unused = nextScene.element.offsetHeight;
        nextScene.element.setAttribute('class', 'run');
    } else {
        console.log(nextScene.element);
        nextScene.element.setAttribute('class', 'run');
    }
    if (this.useOrmma) {
        this.ormmaNextScene(nextScene);
    }
    if (nextScene.startAction) {
        nextScene.startAction(this);
    }
    if (nextScene.animationCount === 0) {
        this.onSceneFinish();
    }
},
replayScene: function () {
    this.startSceneByID(this.currentSceneID);
},
onSceneFinish: function () {
    if (this.scenes[this.currentSceneID].endAction) {
        this.scenes[this.currentSceneID].endAction(this);
    }
},
goToNextScene: function () {
    var nextIndex = this.scenesArray.indexOf(this.scenes[this.currentSceneID]) + 1;
    var nextScene;
    if (nextScene = this.scenesArray[nextIndex]) {
        this.startSceneByID(nextScene.id);
    }
},
goToURL: function (aURL) {
    document.location.href = aURL;
},
ormmaNextScene: function (nextScene) {
    var currentState = ormma.getState();
    if (nextScene.dimensions.expanded) {
        var maxSize = ormma.getMaxSize();
        if (currentState !== 'expanded') {
            ormma.expand({
                x: 0,
                y: 0,
                width: maxSize.width,
                height: maxSize.height
            });
        }
        var transform = "";
        var elementHeight = nextScene.element.offsetHeight;
        var elementWidth = nextScene.element.offsetWidth;
        var y = (maxSize.height - elementHeight) / 2;
        var x = (maxSize.width - elementWidth) / 2;
        transform += " translate3d(" + Math.round(x) + "px," + Math.round(y) + "px,0)";
        if (nextScene.dimensions.fit) {
            var scaleFactor = Math.min(maxSize.width / elementWidth, maxSize.height / elementHeight);
            transform += " scale3d(" + scaleFactor + "," + scaleFactor + ",1)";
        }
        nextScene.element.style.webkitTransform = transform;
    } else {
        if (currentState === 'expanded') {
            ormma.close();
        }
        ormma.resize(nextScene.dimensions.width, nextScene.dimensions.height);
    }
}
};

function loadData() {
var configData = {
    parentId: 'AN-sObj-parentOl',
    ormma: false,
    scenes: [//lots of config data
    ]
  };
  AN.Controller.setConfig(configData);
};

$(document).ready(function() {
   loadData();
   $("area[rel^='prettyPhoto']").prettyPhoto({
      social_tools: '',
      show_title: false,
      theme: 'light_rounded'
   });
});`

The code:

function bindEvent(el, eventName, eventHandler) {
   if (el.addEventListener){
       el.addEventListener(eventName, eventHandler, false); 
   } else if (el.attachEvent){
       el.attachEvent("on"+eventName, eventHandler);
   }
}

is perfectly fine. The only way it fails on if (el.addEventListener) is if el is not a DOM element.

When I go to the actual page you've referenced, I see some javascript errors reported. The first happens because you are passing null for el in addListenerTo() when called from this code:

for (var i = 0; i < this.events.length; i++) {
    event = this.events[i];
    element = document.getElementById(event.id);
    addListenerTo(element, event.type, event.handler);
}

That's because document.getElementById(event.id); does not find an object in the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM