I have been looking through javascript tutorials and can't seem to figure out how to get all of my page to load and then wait a certain amount of time for the footer to load. Any help would be greatly appriciated? HEADER.PHP <html> <head> <title>pages title</title> </head> <body bgcolor="#FFFFFF" text="000000"> Code (markup): BODY.PHP pages content.... Code (markup): FOOTER.PHP //wait specified time then load below <img src="footerimage.gif"> </body> </html> Code (markup):
Well, you can use window.onload = function() { setTimeout("loadFooter()", 5000); //asuming you want to wait 5 seconds after the page has loaded } Code (markup): Now about what loadFooter() does.. I'd do it with AJAX. Send a request for the file footer.php and then append it in the bottom of the page. Another way to do it is add the content of footer.php directly into loadFooter() (asuming the content is what you showed - plain HTML file, with no scripts or something..). Either way you must have something to append the content to. I suggest using a <div> at the end of the other code, which has the id 'footer'. Now here's the javascript example: function loadFooter() { document.getElementById('footer').innerHTML = '<img '+ 'src="footerimage.gif">'+ '</body>'+ '</html>'; } Code (markup): (Damn I hate concatenating javascript strings when posting ) The AJAX way is something like that: function loadFooter() { //send the request, check the readystate, blah blah //and when you get the response do the fallowing document.getElementById('footer').innerHTML = xmlHttp.responseText; } Code (markup): Good luck
Or you could set the footer container's CSS visibility property to "hidden" . Then after certain number of seconds display it using your favorite library or your own JS. YUI example: YAHOO.util.Event.onDOMReady(function() { var showFooter = function() { YAHOO.util.Dom.setStyle("footer_id", "visibility", "visible"); }; setTimeout("showFooter()", 5000) }); Code (markup): Code is untested but should be at least close to working.
This seems to work ok for displaying the footer although I want it to wait to even run the scripts in the footer until after the designated time .... For Example: Page loads completly and displays then wait 10 seconds and add one page view to my database but if the visitor leaves before the 10 seconds then it will not count it as a visit.
Couldn't you just put an ajax call inside the function that gets run with settimeout after 10 seconds? It also depends on what the page being loaded means to you. Does every last file need to be loaded including images or does the DOM just need to be loaded and ready to manipulate, or does one particular area of the page need to be loaded? Here's an untested example that uses YUI and uses the event which is equivalent to window.onload, which means it will wait until everything is loaded. YAHOO.util.Event.addListener(window, "load", function() { var addPageView = function() { var callback = { success: function () { alert('ajax successful!'); }, failure: function () { alert('ajax UNsuccessful!'); } }; YAHOO.util.Connect.asyncRequest('GET', 'http://www.mysite.com/setPageView.php', callback); }; setTimeout("addPageView()", 10000) }); Dependencies: <script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/yahoo/yahoo-min.js" ></script> <script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/event/event-min.js" ></script> <script src="http://yui.yahooapis.com/2.5.2/build/connection/connection-min.js"></script> Code (markup):