1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Javascript works in IE only after adding alert function

Discussion in 'JavaScript' started by MorVimmer, Feb 11, 2008.

  1. #1
    I am baffled!

    I am working on a new website that is based on WordPress.
    To view a PNG image I have added a small script that will show transparent image in IE:
    
    var ie5 = document.all && document.getElementById;
    var ns6 = document.getElementById && !document.all;
    var arVersion = navigator.appVersion.split("MSIE");
    var ieversion = parseFloat(arVersion[1]);
    
    var pngImagesOver = new Array();
    var pngImagesOut = new Array();
    function pngImage(containerId, imagePath, width, height, alt) {
    var container = document.getElementById(containerId);
    if(container) {
    var element;
    
    if ((ieversion >= 5.5) && (ieversion < 7) && (document.body.filters)) {
    
    element = document.createElement('SPAN');
    element.id = containerId + 'Img';
    element.style.display = 'block';
    element.style.width = width;
    element.style.height = height;
    element.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imagePath + "', sizingMethod='scale')";
    }
    else {
    element = document.createElement('IMG');
    element.id = containerId + 'Img';
    element.src = imagePath;
    element.alt = alt;
    element.width = width;
    element.height = height;
    }
    container.appendChild(element);
    }
    }
    
    function pngImageBG(containerId, imagePath, width, height, alt) {
    var container = document.getElementById(containerId);
    if(container) {
    if ((ieversion >= 5.5) && (ieversion < 7) && (document.body.filters)) {
    container.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imagePath + "', sizingMethod='scale')";
    }
    else {
    container.style.backgroundImage = 'url(' + imagePath + ')';
    }
    }
    }
    
    Code (markup):
    This is the call for the function:
    
    <div id="nefrit_image">
    <script type="text/javascript">
    pngImage('nefrit_image', '<?php bloginfo('stylesheet_directory'); ?>/images/nefrit_image.png', 162, 429, 'Nefrit El-Or');
    </script>
    </div>
    
    Code (markup):
    Here is the weird thing:
    The IE browser recognize this script, or whatever other script, ONLY when I add a small, and can be even unrelated, alert box to the javascript page.
    Why it happens?

    Here is the page with the javascript:
    nefritelor.morvimmer.com

    Here is a page in the website that I added an alert box to the same function:
    nefritelor.morvimmer.com/?cat=4


    HELP????

    Thanks
    Mor
     
    MorVimmer, Feb 11, 2008 IP
  2. locdev

    locdev Active Member

    Messages:
    171
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #2
    for me it works under IE6
    may be it cached clientside? try to ctrl+F5
     
    locdev, Feb 11, 2008 IP
  3. MorVimmer

    MorVimmer Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the fast response.
    Not working. Either on my PC at home or at work. Refreshed it multiply times, but still not working.
    Did you viewed both pages that I mentioned?
     
    MorVimmer, Feb 11, 2008 IP
  4. locdev

    locdev Active Member

    Messages:
    171
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
  5. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It's likely a timing issue, I've experienced similar situations with IE. With an alert() statement in there it has time to "catch up".

    The function call is inline at the global scope, and tries to run immediately as soon as the code is encountered. Instead of doing it that way, try putting it in a setTimeout with a delay of 0ms, such as:

    setTimeout("pngImage('nefrit_image', '<?php bloginfo('stylesheet_directory'); ?>/images/nefrit_image.png', 162, 429, 'Nefrit El-Or');", 0);
    Code (markup):
    In most cases this will resolve the problem, but I'm not sure if it will help your particular situation. As locdev pointed out, there are a couple of images which are returning a 500 internal server error.
     
    vpguy, Feb 15, 2008 IP