Solved

Improving Javascript Performance in IE7

Posted on 2008-09-29
5
1,277 Views
Last Modified: 2008-11-05
Can anyone enlighten me on ways to improve javascript performance, particularly for IE7 as the site I am working on looks fine in Firefox, Safari, and Chrome but has some delay in the Javascript procedures with IE7. Most of the javascript is stuff that people have helped me put together over the years from various places on the net and I've modified it where needed to do what I need.

Thanks
window.faderTimeoutsContent = Array()
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style
    object.opacity = (opacity / 100)
    object.MozOpacity = (opacity / 100)
    object.KhtmlOpacity = (opacity / 100)
    object.filter = "alpha(opacity=" + opacity + ")"
}
function hide(id) {
    eval(document.getElementById(id).style.opacity = 0)
    document.getElementById(id).style.MozOpacity = 0
    document.getElementById(id).style.KhtmlOpacity = 0
    document.getElementById(id).style.filter = "alpha(opacity=" + 0 + ")"
    document.getElementById(id).setAttribute("class", "HideDiv");
    document.getElementById(id).setAttribute("className", "HideDiv");
}
function showcontent(id, millisec) {
    for (i in window.faderTimeoutsContent) {
        if (window.faderTimeoutsContent[i]) { clearTimeout(window.faderTimeoutsContent[i]); }
    }
    if (document.getElementById(id).style.opacity == 100) {
        opacity(id, 100, 0, millisec)
    } else {
        document.getElementById(id).setAttribute("class", "ShowDiv");
        document.getElementById(id).setAttribute("className", "ShowDiv");
        opacity(id, 0, 100, millisec)
    }
    function opacity(id, opacStart, opacEnd, millisec) {
        var speed = Math.round(millisec / 100)
        var timer = 0
        if (opacStart > opacEnd) {
            for (i = opacStart; i >= opacEnd; i--) {
                window.faderTimeoutsContent.push(setTimeout("changeOpac(" + i + ",'" + id + "');;", (timer * speed)))
                timer++
            }
        } else if (opacStart < opacEnd) {
            for (i = opacStart; i <= opacEnd; i++) {
                window.faderTimeoutsContent.push(setTimeout("changeOpac(" + i + ",'" + id + "');;", (timer * speed)))
                timer++
            }
        }
    }
}
function opacity(id, opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100)
    var timer = 0
    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i--) {
            window.faderTimeouts.push(setTimeout("changeOpac(" + i + ",'" + id + "');;", (timer * speed)))
            timer++
        }
    } else if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i++) {
            window.faderTimeouts.push(setTimeout("changeOpac(" + i + ",'" + id + "');;", (timer * speed)))
            timer++
        }
    }
}
// JavaScript Document
var timerlen = 7;
var downtimerlen = 10;
var slideAniLen = 500;
var downslideAniLen = 500;
 
var timerID = new Array();
var downtimerID = new Array();
var startTime = new Array();
var downstartTime = new Array();
var obj = new Array();
var downobj = new Array();
var endWidth = new Array();
var downendWidth = new Array();
var moving = new Array();
var downmoving = new Array();
var dir = new Array();
var downdir = new Array();
 
function slideopen(objname) {
    if (moving[objname])
        return;
 
    if (document.getElementById(objname).style.display != "none")
        return; // cannot slide down something that is already visible
 
    moving[objname] = true;
    dir[objname] = "down";
    startslide(objname);
}
 
function slideclosed(objname) {
    if (moving[objname])
        return;
 
    if (document.getElementById(objname).style.display == "none")
        return; // cannot slide up something that is already hidden
 
    moving[objname] = true;
    dir[objname] = "up";
    startslide(objname);
}
 
function slidedown(objname) {
    if (downmoving[objname])
        return;
 
    if (document.getElementById(objname).style.display != "none")
        return; // cannot slide down something that is already visible
 
    downmoving[objname] = true;
    downdir[objname] = "down";
    startslidedown(objname);
}
 
function startslide(objname) {
    obj[objname] = document.getElementById(objname);
 
    endWidth[objname] = parseInt(obj[objname].style.width);
    startTime[objname] = (new Date()).getTime();
 
    if (dir[objname] == "down") {
        obj[objname].style.width = "1px";
    }
 
    if (dir[objname] == "up") {
        obj[objname].style.width = "514px";
    }
 
    obj[objname].style.display = "block";
 
    timerID[objname] = setInterval('slidetick(\'' + objname + '\');', timerlen);
}
 
function startslidedown(objname) {
    downobj[objname] = document.getElementById(objname);
 
    downendWidth[objname] = parseInt(downobj[objname].style.width);
    downstartTime[objname] = (new Date()).getTime();
 
    if (downdir[objname] == "down") {
        downobj[objname].style.height = "1px";
    }
 
    downobj[objname].style.display = "block";
 
    downtimerID[objname] = setInterval('slidetickdown(\'' + objname + '\');', downtimerlen);
}
 
function slidetickdown(objname) {
    var elapsed = (new Date()).getTime() - downstartTime[objname];
 
    if (elapsed > downslideAniLen)
        downendSlide(objname)
    else {
        var d = Math.round(elapsed / downslideAniLen * downendWidth[objname]);
        if (downdir[objname] == "up")
            d = downendWidth[objname] - d;
 
        downobj[objname].style.height = d + "px";
    }
    return;
}
 
function slidetick(objname) {
    var elapsed = (new Date()).getTime() - startTime[objname];
 
    if (elapsed > slideAniLen)
        endSlide(objname)
    else {
        var d = Math.round(elapsed / slideAniLen * endWidth[objname]);
        if (dir[objname] == "up")
            d = endWidth[objname] - d;
 
        obj[objname].style.width = d + "px";
    }
 
    return;
}
 
function endSlide(objname) {
    clearInterval(timerID[objname]);
 
    if (dir[objname] == "up")
        obj[objname].style.display = "none";
 
    obj[objname].style.width = endWidth[objname] + "px";
 
    delete (moving[objname]);
    delete (timerID[objname]);
    delete (startTime[objname]);
    delete (endWidth[objname]);
    delete (obj[objname]);
    delete (dir[objname]);
 
    return;
}
 
function downendSlide(objname) {
    clearInterval(downtimerID[objname]);
 
    if (downdir[objname] == "up")
        downobj[objname].style.display = "none";
 
    downobj[objname].style.height = downendWidth[objname] + "px";
 
    delete (downmoving[objname]);
    delete (downtimerID[objname]);
    delete (downstartTime[objname]);
    delete (downendWidth[objname]);
    delete (downobj[objname]);
    delete (downdir[objname]);
 
    return;
}
var x = 0; //Starting Location - left
var y = 0; //Starting Location - top
var dest_x = 340;  //Ending Location - left
var interval = 10; //Move 10px every initialization
 
function moveImage() {
    //Keep on moving the image till the target is achieved
    if (x < dest_x) x = x + interval;
    //Move the image to the new location
    document.getElementById("menu_options").style.left = x + 'px';
 
    if (x + interval < dest_x) {
        //Keep on calling this function every 100 microsecond 
        //	till the target location is reached
        window.setTimeout('moveImage()', 10);
    }
}
 
var abx = -200; //Starting Location - left
var abdest_x = 330;  //Ending Location - left
var abinterval = 10; //Move 10px every initialization
 
function moveaboutImage() {
    //Keep on moving the image till the target is achieved
    if (abx < abdest_x) abx = abx + abinterval;
    //Move the image to the new location
    document.getElementById("menu_options").style.left = abx + 'px';
 
    if (abx + abinterval < abdest_x) {
        //Keep on calling this function every 100 microsecond 
        //	till the target location is reached
        window.setTimeout('moveaboutImage()', 10);
    }
}
/*
 
Correctly handle PNG transparency in Win IE 5.5 & 6.
http://homepage.ntlworld.com/bobosola. Updated 18-Jan-2006.
 
Use in <HEAD> with DEFER keyword wrapped in conditional comments:
<!--[if lt IE 7]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]-->
 
*/
 
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])
 
if ((version >= 5.5) && (document.body.filters)) {
    for (var i = 0; i < document.images.length; i++) {
        var img = document.images[i]
        var imgName = img.src.toUpperCase()
        if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
            img.outerHTML = strNewHTML
            i = i - 1
        }
    }
}
function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}
function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}
 
function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

Open in new window

0
Comment
Question by:Curtis_Shirley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 22603512
Use flash.

You have too much going on on that page
0
 

Author Comment

by:Curtis_Shirley
ID: 22605048
Ok, let me scrap everything I've done then and get right on that... thanks but I'd prefer not to have to learn another programming language just to make sites work nicely in IE.
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 350 total points
ID: 22605076
Seriously. I count sliding, opacity change filters on all images png fixing and so on.

I would be scared to open that page.

Perhaps use one or two things but 4 or 5 and some are similar using different code?

At least make the opacity change use the IE built-in fading
0
 
LVL 42

Assisted Solution

by:David S.
David S. earned 150 total points
ID: 22612894
For one thing, AlphaImageLoader, is for IE5.5 and IE6 only. IE7 natively supports the alpha channel on PNG-24s and thus doesn't need it.

Another thing I would change is

    document.getElementById(id).setAttribute("class", "HideDiv");
    document.getElementById(id).setAttribute("className", "HideDiv");

to just:

    document.getElementById(id).setAttribute("class", "HideDiv");
    document.getElementById(id).className = "HideDiv";

or even better would be to use an addClass function, so you support elements with multiple classes.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 350 total points
ID: 22613138
Not to mention

    eval(document.getElementById(id).style.opacity = 0)
    document.getElementById(id).style.MozOpacity = 0
    document.getElementById(id).style.KhtmlOpacity = 0
    document.getElementById(id).style.filter = "alpha(opacity=" + 0 + ")"
    document.getElementById(id).setAttribute("class", "HideDiv");
    document.getElementById(id).setAttribute("className", "HideDiv");

can be written
  var obj =  document.getElementById(id); // get it ONCE
  var theStyle = obj.style;
  theStyle.opacity = theStyle.MozOpacity = theStyle.KhtmlOpacity = 0;
  if (theStyle.filter) theStyle.filter = "alpha(opacity=0)"
  obj.className="HideDiv";

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question