SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    857
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    function output a value

    I have a "on/off" switch and I am trying to print either "on" or "off" into a piece of JavaScript used to write object code for Flash (flash object). How can I have getStatus() below output "on" or "off"? Is there a better way to do this?

    Code JavaScript:
    <html>
    <head>
    <script type="text/javascript" src="/flashobject.js"></script>
    </head>
     
    <body>
     
    <script>
    var width = 1;
     
    function getStatus() {
      if (width == 1) {
    	  return statSwitch = "off";
    	} else {
    	  return statSwitch = "on";
    	}
    }
     
    // I want getStatus() below to output either "on" or "off" so that the output is "../gallery_ads.swf?SWITCH=on" for example
    var fo = new FlashObject("../gallery_ads.swf?SWITCH="getStatus(), "Gallery", "835", "525", "8", "#D5CDA0");
    fo.write("flashcontent");
    </script>
     
    <div id="flashcontent"><strong>You need to upgrade your Flash Player</strong></div>
     
    </body>
    </html>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Code JavaScript:
    var fo = new FlashObject("../gallery_ads.swf?SWITCH=" + getStatus(), "Gallery", "835", "525", "8", "#D5CDA0");

  3. #3
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    857
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    One more..

    Thanks... I am getting a javascript error saying that statSwitch is undeclaired using the following code:

    Code JavaScript:
    <html>
    <head>
    <script>
    var width = 1;
     
    function processImage() {
      if (width == 1) {
    	  return statSwitch = "off";
    	} else {
    	  return statSwitch = "on";
    	}
    }
    </script>
    </head>
     
    <body onload="processImage()">
    <script>
    // returns an error: "statSwitch is not defined on document.write(statSwitch);"
    document.write(statSwitch);
    </script>
    </body>
    </html>

    Can I do something equivalent to "var statSwitch .= "";" to keep it as placeholder?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Yes, place it outside of the function, just below the width value

  5. #5
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    857
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like this? Its not working- anywhere I put it or use += or .=

    Code JavaScript:
    <html>
    <head>
    <script>
    var width = 1;
     
    function processImage() {
      if (width == 1) {
    	  // bugger off
    	  return statSwitch = "off";
     
    	} else {
    	  // use it
    	  return statSwitch = "on";
     
    	}
    }
    var statSwitch .= "";
    </script>
    </head>
     
    <body onload="processImage()">
    <script>
    document.write(statSwitch);
    </script>
    </body>
    </html>

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    When you use var you are limiting the scope of that variable to the area that it's in.

    Do it like the following:

    Code Javascript:
    statSwitch = 'on';
    var width = 1;
     
    function processImage() {
      if (width == 1) {
          // bugger off
          statSwitch = "off";
        } else {
          // use it
          statSwitch = "on";
        }
    }

  7. #7
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    857
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I do that, it outputs "on" by default, and ignore the processImage script to set the statSwitch. Technically, this should output "off" because width=1 ... but its not

    Code JavaScript:
    <html>
    <head>
    <script>
    statSwitch = "on";
    var width = 1;
     
    function processImage() {
      if (width == 1) {
          // bugger off
          statSwitch = "off";
        } else {
          // use it
          statSwitch = "on";
        }
    }
    </script>
    </head>
     
    <body onload="processImage()">
    <script>
    document.write(statSwitch);
    </script>
    </body>
    </html>

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    The output is occurring before the onload event has taken place.

    Run the function from the end of the document instead, before you show the results of its value.

    Code HTML4Strict:
    <body>
    <script>
    processImage();
    document.write(statSwitch);
    </script>

  9. #9
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    857
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but when I do that it causes the FireFox bug and it thinks the image is 0 when it should be 1.

    This is my final challenge. I need to output the value 'on' or 'off' when the processImage() is fired from the body. This is the only time the value is correct. Only when the image is called from the body can firefox get its proper value. Perhaps how can I store this value to write 'on' or 'off'?

    Code JavaScript:
    <html>
    <head>
    <script>
    var statSwitch = "";
    // this image below is 1x1px
    var url = 'http://ad.doubleclick.net/ad/website.com/;sect=;subs=;sz=115x77;ord=4771639';
     
    var img = document.createElement('img');
    img.setAttribute('id', 'testImage');
    img.setAttribute('src', url);
     
    function processImage() {
    alert(img.width);  
     
    if (img.width == 1) {
          statSwitch += "off"; // how can i output this value or store it to use later?
        } else {
          statSwitch += "on"; // how can i output this value or store it to use later?
        }
    }
     
    </script>
    </head>
     
    <body onload="processImage()">
    <script>
    // processImage()   <--- when used it causes the firefox bug
    document.write(statSwitch); // Need this to output 'on' or 'off'
    </script>
    </body>
    </html>

  10. #10
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    857
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice, it works in FF, but now its not working in IE!

    launches active content warning, then it throws a js error "img.width is null or not an object"

    ..any ideas?

    BTW, YOU RULE!

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by ripcurlksm View Post
    I need to output the value 'on' or 'off' when the processImage() is fired from the body.
    The onload="processImage()" occurs after the document.write occurs.

    Wrap your head around that idea. You are wanting to write the result of a function before that function has had the chance to run. It quite simply can't be done.

    What you will need to do is to write the value afterwards. Here is how I would do that.

    Code HTML4Strict:
    <body onload="initImage()">
    <div id="statswitch"></div>
    </body>

    With the following script

    Code JavaScript:
    statSwitch = '';
    // this image below is 1x1px
    var url = 'http://ad.doubleclick.net/ad/website.com/;sect=;subs=;sz=115x77;ord=4771639';
     
    var img = document.createElement('img');
    img.setAttribute('id', 'testImage');
    img.setAttribute('src', url);
     
    function initImage() {
      processImage();
      outputStat();
    }
    function processImage() {
      if (img.width == 1) {
        statSwitch = "off";
      } else if (img.width > 1) {
        statSwitch = "on";
      }
    }
    function outputStat() {
      var stat = document.getElementById('statswitch');
      var statText = document.createTextNode(statSwitch);
      if (stat && stat.firstChild) {
        stat.replaceChild(statText, stat.firstChild);
      } else if (stat) {
        stat.appendChild(statText);
      }
    }

    If the value is to be used with another element on the page, then you can use similar techniques to update the content of that element.

  12. #12
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    The code I posted the snippets from worked fine in IE.

    Pleas repost your full test code and we'll see where the trouble lays.

  13. #13
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    857
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) It does work! I do get an ActiveX warning in IE however. If I allow the blocked content it prints correctly. Do you get this blocked content warning? Im on IE 7.0 PC

    2) Can you explain the outputStat function? I love it

    3) Here is my code (is it better to move these functions to the header like this?):
    Code JavaScript:
    <html>
    <head>
    <script type="text/javascript">
    statSwitch = "";
     
    var url = 'http://ad.doubleclick.net/ad/website.com/;sect=;subs=;sz=115x77;ord=4771639';
     
    var img = document.createElement('img');
    img.setAttribute('id', 'testImage');
    img.setAttribute('src', url);
     
    function processImage() {
      if (img.width == 1) {
        statSwitch = "off";
      } else if (img.width > 1) {
        statSwitch = "on";
      }
    }
    function outputStat() {
      var stat = document.getElementById('statSwitch');
      var statText = document.createTextNode(statSwitch);
      if (stat && stat.firstChild) {
        stat.replaceChild(statText, stat.firstChild);
      } else if (stat) {
        stat.appendChild(statText);
      }
    }
    function initImage() {
      processImage();
      outputStat();
    }
    </script>
    </head>
     
    <body onload="initImage()">
    <div id="statSwitch"></div>
    </body>
    </html>

  14. #14
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    You can move them to the header, but it will take longer for people to see the webpage because everything is put on hold until the head scripts have been loaded.

    IE gives a warning about running scripts or activex stuff when you run them locally, just as an advisory. There will be no warning when you upload them to a server.

    You're wanting me to explain the outputStat function.

    Code Javascript:
    function outputStat() {
      var stat = document.getElementById('statswitch');
      var statText = document.createTextNode(statSwitch);
      if (stat && stat.firstChild) {
        stat.replaceChild(statText, stat.firstChild);
      } else if (stat) {
        stat.appendChild(statText);
      }
    }

    The example could have been simpler by replacing the whole if block with the statement stat.appendChild(statText);

    The reason for having it there though, is to allow for the possibility of the value being updated several times from the same page.

    The if block checks to see if the element called stat exists. If it doesn't exist it will quietly fail.

    If the stat element does exist, it checks to see if there is anything inside it already. If the div already has a something in there, for example, a default value before the check, it will replace that.

    If the stat element is empty then the value is added to it.

    It could have also been written another way as follows, but in this situation I preferred the above code with less nesting complicating things.

    Code Javascript:
    function outputStat() {
      var stat = document.getElementById('statswitch');
      var statText = document.createTextNode(statSwitch);
      if (stat) {
        if (stat.firstChild) {
          stat.replaceChild(statText, stat.firstChild);
        } else {
          stat.appendChild(statText);
        }
      }
    }

    It can also be written even easier while achieving the same results, by moving the complicated hard work outside to another function, where you can use it again and again from one project to the next.

    Code Javascript:
    function outputStat() {
      updateElement(
        document.getElementById('statswitch'),
        document.createTextNode(statSwitch)
      );
    }
    function updateElement(el, value) {
      if (el) {
        if (el.firstChild) {
          el.replaceChild(value, el.firstChild);
        } else {
          el.appendChild(value);
        }
      }
    }

  15. #15
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    857
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the great explanation!

    Do you recommend using javascript to load large elements to make the page load faster and balance the load?

  16. #16
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    That's only a good idea if the isn't going to be used until later on after the page has loaded.

  17. #17
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    857
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do I have to use the div tag to print the variable? Can I call it by name and pull it in?

    or can I do this
    Code JavaScript:
    <script>
    var fo = new FlashObject("../gallery_ads.swf?SWITCH=<div id="statSwitch"></div>", "Gallery", "835", "525", "8", "#D5CDA0");
    fo.write("flashcontent");
    </script>

  18. #18
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    The div tag was just an example of updating the information on a page.

    With your example there you will need to start the flash object in one state, and then later when the check has been performed, update the flash object.

    You can do this most easily by passing the state to a function that will create the flash for you.

    Code Javascript:
    function adsGallery(state) {
      if (state != 'on' || state != 'off') {
        // default value
        state = 'on';
      }
      var fo = new FlashObject("../gallery_ads.swf?SWITCH=" + state, "Gallery", "835", "525", "8", "#D5CDA0");
      fo.write("flashcontent");
    }

    Checking the state value isn't strictly necessary, but it's a nice reminder of the allowed values, and it also means you can call adsGallery() with no parameters to achieve the default results.

    Now from somewhere in your code you can call that and update it.

    Code Javascript:
    // start with it off
    adsGallery('off');
     
    // . . .
     
    // and when conditions are right
    adsGallery('on');

  19. #19
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    857
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks pmw57, I am used to being able to declaire a vaule with PHP and use it though out the page when called.

    I appreciate all of your help.

    Merry Christmas,
    Kevin

  20. #20
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    If there's anything else, please don't hesitate to let us know about it and we'll do what we can to get you right.

    And christmas greetings to you and all, too.

  21. #21
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    857
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I have it working - to cap this off would you say this is the best structure/way to do all this?

    Code JavaScript:
    <html>
    <head>
    </head>
    <body onLoad="initImage()">
    <script>
    // ON
    // var url = 'http://www.sitepoint.com/forums/image.php?u=52542&dateline=1108372778';
     
    // OFF
    var url = 'http://ad.doubleclick.net/ad/website.com/;sect=;subs=;sz=115x77;ord=4771639';
     
    var img = document.createElement('img');
    img.setAttribute('id', 'testImage');
    img.setAttribute('src', url);
     
    function processImage() {
      if (img.width == 1) {
        statSwitch = "off";
      } else if (img.width > 1) {
        statSwitch = "on";
      }
    }
    function outputStat() {
      var stat = document.getElementById('statSwitch');
      var statText = document.createTextNode(statSwitch);
      if (stat && stat.firstChild) {
        stat.replaceChild(statText, stat.firstChild);
      } else if (stat) {
        stat.appendChild(statText);
      }
    }
    function initImage() {
      processImage();
      outputStat();
      adsGallery(statSwitch);
    } 
     
    function adsGallery(state) {
      var fo = new FlashObject("../gallery_ads.swf?SWITCH=" + state, "Gallery", "835", "525", "8", "#D5CDA0");
      fo.write("flashcontent");
    }
    </script>
    <div id="flashcontent"><strong>You need to upgrade your Flash Player</strong></div>
    </body>
    </html>

  22. #22
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    That onload part is going to wait for all images to have finished loading before it activates. Instead, with the script at the bottom of the page, it will automatically run as soon as the dom is loaded, which is even earlier than the onload event.

    But you don't want it to run before the particular image has loaded, so all you need to do is to attach an onload event to the image itself.

    While it is possible to do this with something like following code, I wouldn't advise doing it this way.

    Code JavaScript:
    if (window.addEventListener) {
      img.addEventListener('load', initImage, false);
    } else if (window.attachEvent) {
      img.attachEvent('onload, initImage);
    }

    Instead, there is a much easier way where you make use of a clever interface to do the heavy lifting instead.

    Code JavaScript:
    Event.add(img, 'load', initImage);

    This becomes possible thanks to a very clever and useful interface to add and remove elements from Dustin Diaz, that you can place elsewhere.

    Code JavaScript:
    var Dom = {
      get: function(el) {
        if (typeof el === 'string') {
          return document.getElementById(el);
        } else {
          return el;
        }
      },
      add: function(el, dest) {
        var el = this.get(el);
        var dest = this.get(dest);
        dest.appendChild(el);
      },
      remove: function(el) {
        var el = this.get(el);
        el.parentNode.removeChild(el);
      }
    };
    var Event = {
      add: function() {
        if (window.addEventListener) {
          return function(el, type, fn) {
            Dom.get(el).addEventListener(type, fn, false);
          };
        } else if (window.attachEvent) {
          return function(el, type, fn) {
            var f = function() {
              fn.call(Dom.get(el), window.event);
            };
            Dom.get(el).attachEvent('on' + type, f);
          };
        }
      }()
    };

    With that in place, other parts of the code can then be updated to use it instead, and the interface can be extended to allow you to more easily perform other DOM manipulations.

    Another part that I'm not happy with is the outputStat function. It should instead create a clone of statSwitch with the updated content then just replace the old one with the new one, which would be a lot more tidier. It may even be possible for me to explore a few of these improvements in the weekend.

  23. #23
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    857
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would be very interested if you did...

  24. #24
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Okay, here is what I've done to improve on the above code.

    Along with the swfobject.js file, another supporting script is common.js which is where most of the Dom code has gone.

    Previously, the default text for the flash content was shown on the page until all of the content for the page had being loaded.

    Code Javascript:
    // turn off the gallery (and default flashcontent message)
    adsGallery('off');

    When the gallery is temporarily turned off, this instantly removes the default message from the screen, and the gallery is then turned on when the temporary image determines that it's okay to do so.

    Creating the test image is now very simple thanks to our new Dom interface.
    Code Javascript:
    var testImg = Dom.create('img', {id: 'testImage'}, {src: testUrl('image')});

    The Dom interface from before has been extended in order to help remove complexity from the main code. Along with the original get, add and remove methods, there is also now a create method and attribute get and set methods.

    These methods can be easily updated, which provides a solid set that can be relied upon to do a great job both now and in the future.

    For example, here is the create function that was used above.
    Code Javascript:
    ...
    create: function (tag) {
        var el = document.createElement(tag);
        forEach(arguments, function (arg, index) {
            if (index > 0) {
                Dom.attr.set(el, arg);
            }
        });
        return el;
    },
    ...

    The above function also makes good use of a wonderful forEach function from Dean Edwards, about Enumerating Javascript Objects.

    Once the test image has been loaded, it initializes the gallery

    Code Javascript:
    Event.add(testImg, 'load', initGallery);

    When the initGallery function is called, the this keyword refers to the image itself, making it very easy to check the image size.
    adsGallery can then be called with the appropriate value.

    Code Javascript:
    function initGallery() {
        // Disable the ads if there's nothing to be shown
        adsGallery((this.width === 1) ? 'off' : 'on');
    }

    So in full, the code for adsgallery.js is now a lot easier to understand

    Code Javascript:
    // turn off the gallery, which also speeds up removing of flashcontent message
    adsGallery('off');
     
    // create test image and init the gallery when loaded
    var testImg = Dom.create('img', {id: 'testImage'}, {src: testUrl('image')});
    Event.add(testImg, 'load', initGallery);
     
    function testUrl(type) {
        // provide a full-sized image with dimensions, or a blank image at size 1x1
        if (type) {
            return 'http://www.sitepoint.com/forums/image.php?u=52542&dateline=1108372778';
        } else {
            return 'http://ad.doubleclick.net/ad/website.com/;sect=;subs=;sz=115x77;ord=4771639';
        }
    }
    function initGallery() {
        // Disable the ads if there's nothing to be shown
        adsGallery((this.width === 1) ? 'off' : 'on');
    }
    function adsGallery(state) {
        // display the shockwave flash gallery
        var fo = new FlashObject("../gallery_ads.swf?SWITCH=" + state, "Gallery", "835", "525", "8", "#D5CDA0");
        fo.write("flashcontent");
    }

    here is the HTML, with the scripts being loaded at the bottom, which avoids any slow down from the previous onload event that used to be on the body.

    Code HTML4Strict:
    <html>
    <head>
    </head>
    <body>
    <div id="flashcontent"><strong>You need to upgrade your Flash Player</strong></div>
    <script type="text/javascript" src="common.js"></script>
    <script type="text/javascript" src="swfobject.js"></script>
    <script type="text/javascript" src="adsgallery.js">
    </script>
    </body>
    </html>

    And here is the common.js file which does the heavy lifting.

    Code Javascript:
    var Dom = {
        get: function (el) {
            if (typeof el === 'string') {
                return document.getElementById(el);
            } else {
                return el;
            }
        },
        add: function (el, dest) {
            el = this.get(el);
            dest = this.get(dest);
            dest.appendChild(el);
        },
        remove: function (el) {
            el = this.get(el);
            el.parentNode.removeChild(el);
        },
        create: function (tag) {
            var el = document.createElement(tag);
            forEach(arguments, function (arg, index) {
                if (index > 0) {
                    Dom.attr.set(el, arg);
                }
            });
            return el;
        },
        attr: {
            set: function (el, object) {
                el = Dom.get(el);
                var name, value;
                for (name in object) if (object.hasOwnProperty(name)) {
                    value = object[value];
                    el.setAttribute(name, value);
                }
            },
            get: function (el, param) {
                el = Dom.get(el);
                return el.getAttribute(param);
            }
        }
    };
    var Event = {
        add: function () {
            if (window.addEventListener) {
                return function (el, type, fn) {
                    Dom.get(el).addEventListener(type, fn, false);
                };
            } else if (window.attachEvent) {
                return function (el, type, fn) {
                    var f = function () {
                        fn.call(Dom.get(el), window.event);
                    };
                    Dom.get(el).attachEvent('on' + type, f);
                };
            }
        }()
    };
    // generic enumeration
    Function.prototype.forEach = function (object, block, context) {
        for (var key in object) {
            if (typeof this.prototype[key] === "undefined") {
                block.call(context, object[key], key, object);
            }
        }
    };
    // globally resolve forEach enumeration
    var forEach = function (object, block, context) {
        if (object) {
            var resolve = Object; // default
            if (object instanceof Function) {
                // functions have a "length" property
                resolve = Function;
            } else if (object.forEach instanceof Function) {
                // the object implements a custom forEach method so use that
                object.forEach(block, context);
                return;
            } else if (typeof object.length === "number") {
                // the object is array-like
                resolve = Array;
            }
            resolve.forEach(object, block, context);
        }
    };
    Last edited by paul_wilkins; Jan 4, 2008 at 14:01.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •