sw9 Posted July 1, 2009 Share Posted July 1, 2009 Hi, I am pretty new to javascript and I am trying to get some standalone code working properly so that I can make a widget for a population counter. So first I am just trying to isolate some existing code on its own page to make sure it's work. If you go to this website (http://www.populationmedia.org/), you will see a counting population clock. I am trying to recreate that here: http://populationmedia.org/countertest.html I did not create the original clock, but so far as I can tell, I have isolated the necessary code to get this done. Yet it is not working on my test page. I have looked through the code and don't see it calling anything else external. The javascript is exactly the same. Any ideas on what I'm doing wrong? The code I am using for my clock is below: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head profile="http://gmpg.org/xfn/11"> <title>Test</title> <style type="text/css"> p.currentPop { font-family: verdana; font-size: 10px; margin: 0 0 0 15px; padding: 0; color: #13365E; } input.currentPopNum { font-family: georgia; font-size: 28px; margin: 0 0 0 15px; padding: 0; border: 0; color: #13365E; } p.netPop { font-size: 10px; font-family: verdana; margin: 10px 0 0 15px; padding: 0; color: #237114; } input.netPopNum { font-family: georgia; font-size: 28px; margin: 0 0 0 15px; padding: 0; border: 0; color: #237114; } </style> <script language="javascript" type="text/javascript"> // this code is for the counter =========================== // This next little bit of code tests whether the user accepts cookies. var WM_acceptsCookies = false; if(document.cookie == '') { document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie. if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) { WM_acceptsCookies = true; }// If it succeeds, set variable } else { // there was already a cookie WM_acceptsCookies = true; } function WM_setCookie(name, value, path) { if (WM_acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies. document.cookie = name + '=' + escape(value) + ((path)?';path=' + path:''); // Set the cookie, adding any parameters that were specified. } } // WM_setCookie function WM_readCookie(name) { if(document.cookie == '') { // there's no cookie, so go no further return false; } else { // there is a cookie var firstChar, lastChar; var theBigCookie = document.cookie; firstChar = theBigCookie.indexOf(name); // find the start of 'name' var NN2Hack = firstChar + name.length; if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie firstChar += name.length + 1; // skip 'name' and '=' lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';'). if(lastChar == -1) lastChar = theBigCookie.length; return unescape(theBigCookie.substring(firstChar, lastChar)); } else { // If there was no cookie of that name, return false. return false; } } } // WM_readCookie function Comma(number) { number = '' + number; if (number.length > 3) { var mod = number.length % 3; var output = (mod > 0 ? (number.substring(0,mod)) : ''); for (i=0 ; i < Math.floor(number.length / 3); i++) { if ((mod == 0) && (i == 0)) output += number.substring(mod+ 3 * i, mod + 3 * i + 3); else output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3); } return (output); } else return number; } function start() { if(document.cookie.indexOf("cookietime") == -1) { startdate = new Date(); WM_setCookie("cookietime", startdate.getTime(), "/"); } cookietime = WM_readCookie("cookietime"); now(); } function now() { var startpopulation = 6016722474.0; var growthpercentage = 0.01387645; var bornpersecond = (startpopulation * growthpercentage)/365.0/24.0/60.0/60.0; currentdate = new Date (); basedate = new Date (2000,0,1); totalpop= (currentdate.getTime() - basedate.getTime())/1000*bornpersecond + startpopulation; totalpop=Math.floor(totalpop); totalpop=Comma(totalpop); document.Values.Population.value =totalpop; secondsinsession = (currentdate.getTime() - cookietime)/1000; sessionpopulation = secondsinsession*bornpersecond; sessionpopulation = Math.round(sessionpopulation)+10000000000; sessionpopulation = sessionpopulation.toString(); sessionpopulation = sessionpopulation.slice(1,11); sessionpopulation=Comma(sessionpopulation); document.Values.Netgrowth.value =sessionpopulation; the_timeout=setTimeout("now()",50); } </script> </head> <body> <table width="630" border="0" style="margin-top: 5px;"> <tr> <td valign="top" style="border-left: 1px solid #CCC;"> <form name="Values"> <p class="currentPop">CURRENT WORLD POPULATION</p> <INPUT TYPE="text" class="currentPopNum" name="Population" size="14"> <p class="netPop">NET GROWTH DURING YOUR VISIT</p> <input type="text" class="netPopNum" name="Netgrowth" size="14"> </form> </td> </tr> </table> </body> </html> thanks in advance!!! Quote Link to comment https://forums.phpfreaks.com/topic/164391-javascript-widget-confusion/ Share on other sites More sharing options...
Adam Posted July 2, 2009 Share Posted July 2, 2009 From what I can see, you're not actually calling the start() function at any point... Either in the <body> 'onload' attribute, or using 'window.onload' you need to call the start() function. Quote Link to comment https://forums.phpfreaks.com/topic/164391-javascript-widget-confusion/#findComment-867664 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.