Jump to content

Javascript Widget Confusion


sw9

Recommended Posts

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!!!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.