This:
Code:
(function(){...})()
usually with an appropriate terminus at the end (, or ;) depending upon where it appears in the code, unless as the last property/item in a literal object or array, is the self executing anonymous function. It is used twice in the code. First as a container/wrapper to limit the scope of the code. It makes the code entirely private, so as that its variable and function names cannot conflict with any other code you may add to the page. Second as a function to return one of two possible functions, depending upon which method the browser supports for adding/attaching events, or an empty function if neither is supported. All modern browsers (from Firefox 1, IE 5, etc.) support one or the other or both. So if the browser supports addEventListener, it will use that, otherwise, it is most likely IE, but whatever it is, it will use attachEvent if available in it. Legacy browsers will not run the code, and don't support iframe anyway. This is done one time as the code loads, so after that, no branching is required. The method a browser uses to do this cannot change once the page has loaded, so no need to branch each time this type of thing is needed.
That should answer your questions, but if you have more on that or anything else, feel free to ask. Here's the updated code, fulfilling what I now believe to be your requirements. Notice its configuration area at the end:
Code:
(function(){
var overRate, indRate, frame, i, pages = [], attach = (function(){
return window.addEventListener? function(el, ev, f){el.addEventListener(ev, f, false);} :
window.attachEvent? function(el, ev, f){el.attachEvent('on' + ev, f)} : function(){return;};
})();
function updateFrame(){
if (++i < pages.length){
setTimeout(function(){frame.src = pages[i % pages.length];}, indRate * 1000);
}
}
function load(init){
if(init !== null){
frame = document.getElementById('iFrame');
attach(frame, 'load', updateFrame);
}
i = 0;
frame.src = pages[0];
setTimeout(function(){load(null);}, overRate * 1000);
};
attach(window, 'load', load);
//Configure Rates:
overRate = 60; //seconds between over all cycles
indRate = 5; //seconds between individual iframe page changes
//Configure Pages:
pages[0] = "http://www.dynamicdrive.com";
pages[1] = "http://www.bing.com";
pages[2] = "http://www.ask.com";
pages[3] = "http://www.altavista.com";
})();
The rest of the page has not changed. Except you must now remove:
HTML Code:
<meta http-equiv="refresh" content="60">
But unless required for some other code, you may also remove the highlighted:
Code:
<iframe id="iFrame" src="about:blank" width="100%"
height="100%" name="youriframe" frameborder="1" vspace="0" hspace="0"
marginwidth="0" marginheight="0" scrolling="yes" noresize>
</iframe>
Bookmarks